Skip to main content

Posts

Showing posts from November, 2013

C# Tutorial - Poking at Event Contents [Advanced]

Events in C# always feel like there is a little touch of black magic in the background keeping things running smoothly. We have had tutorials here before on events in C# - we took a look at how to create your own custom events in C# Snippet Tutorial - Custom EventHandlers , and we looked at the syntactic sugar behind the += and -= operators for events in C# Tutorial - EventAccessors . But we have never taken a look at what actually happens when you declare an event, and what happens when you invoke it. The key behind events in C# in the MulticastDelegate . You've probably seen delegates before (if you haven't, they are just a reference to a method), and knowing that, you can probably wager a guess as to what a MulticastDelegate is. A MulticastDelegate is essentially a list of method references that acts like a regular old delegate in many ways. For instance, take a look at the following: private void DoIt () { var del = new Action ( Method1 ); del ()...

C# Tutorial - Object Finalizers [Advanced]

Recently, in a tutorial about WeakReferences in C#, we talked a bit about garbage collection and how the garbage collector works in .NET. I figured since we already started addressing that stuff, there is no reason not to delve deeper. And so, today we are going to take a look at how object finalizers work in C#. What is an object finalizer? I'm glad you asked! They are essentially the cleanup functions for classes - when an object is collected by the garbage collector in .NET, the finalizer gets run, hopefully cleaning up any unmanaged resources that object may have been holding on to (file references, window handles, network sockets, etc...). An object finalizer is in may ways similar to the C++ destructor , but unlike in C++, a programmer can never call a finalizer directly (in C++, there is the delete operator, and .NET has no such equivalent). So, first, lets take a look at how to write a finalizer, and then we can delve into the details on when they are run and...

C# Tutorial - Weak References [Advanced]

We all know (hopefully) that C# is a garbage-collected language. In general, what this means is that we as programmers don't need to free our own memory - the garbage collector will free that memory for us once it is no longer being referenced. Now, of course, garbage collection is a lot more complicated than that, and writing a good garbage collector is actually a relatively hard problem. And the fact that writing a perfect garbage collector is probably impossible is the reason why things like C#'s Weak Reference object exist. Generally, when you talk about a reference to an object in .NET (and in most other garbage collected languages), you are talking about a "strong" reference. As long as that reference exists, the garbage collector won't collect the object behind that reference. A weak reference is a special type of reference where the garbage collector can still collect the underlying object, even though you still technically have a reference to it....

C# WPF Tutorial - Print Queues And Capabilities [Intermediate]

We have taken a look at printing in WPF twice before here at SOTC - first with a simple tutorial on just getting somethingprinted , and then a more complex one on pagination . Today we are not going to focus much on the printing side of things, but more on the printer side. For example, how do you get a list of the printers available on the system? Or their capabilities? If you need the answers to those questions, then this is the tutorial for you. Today, we will be creating a little sample application that finds all the printers on your system (both local and network printers) and lists them out. When you pick a particular printer, you will get a list of the supported page sizes for that printer. Once you pick a page size, you can then print a test page to the chosen printer at the chosen page size (and you can even pick landscape or portrait). Oh, and did I mention that we never have to show the standard print dialog for any of this? Ok, to start off we first have ...

C# WPF Tutorial - Dynamic Data and the TreeView [Intermediate]

One WPF control that we haven't taken a look at here is the TreeView . Well, no more! Today we are going to rectify that, as we build an application that not only uses the TreeView , but also dynamically loads data into it on demand. We are going to cover a couple other new topics as well, including HierarchicalDataTemplates and CompositeCollections . So what are we building? A pretty simple app that pulls the tree hierarchy of categories and images from GamingTextures and displays it in a TreeView . Gaming Textures has a couple of calls that we can make to get lists of base categories and then the children for each category - so we will be making a web request on demand to get the children for a category, parsing the resulting JSON into C# objects, and then adding those items to the tree view. For example, we start out with the list of base categories: When an item is expanded, we send off a request for the children: And once we have the children, we...