Towards cleaner code, a C# Async. Helper for GUI (Pt 3 of 3)
Posted by Nicholas Brookins on 27 September, 2008 - 8 responses// Threading and User Interfaces
When doing GUI development on Windows you must very careful what thread you access a form or control from. Hopefully most Windows Forms developers know this by now, but in the early versions of the framework it could be a sneaky issue. As of .NET 2.0 they were kind enough to immediately cause an exception when a control was accessed from the wrong thread - forcing you to deal with it now instead of trying to figure out why you randomly get exceptions like this 6 weeks after deploying your application: 
I wont delve much in to why this is necessary, there are plenty of explanations on the subject. The short story is that in .NET there is still a message loop that is running on the thread your form was run from, access from any other thread just isn’t thread-safe - you can’t use locking because it would block the user interface.
So let’s append the Async helper static class that we worked on in parts 1 and 2, giving it the ability to schedule tasks on the UI thread. This will go even further towards making an app cleaner and more maintainable.
UPDATE: This post is now an article on CodeProject.
More…
Towards cleaner code, a C# Asynchronous Helper (Pt 2 of 3)
Posted by Nicholas Brookins on 24 September, 2008 - 2 responses// The Code.
When we last were together, I spent entirely too long explaining the wrong and/or hard ways to start a quick asynchronous task in C#, and only gave a quick taste of what was to come with the Async helper class. This time I’ve got the source to back me up.
UPDATE: The first part of this series is now an article on CodeProject. Check it out and give it a vote for me.
// The main method: Async.Do(…); Making one method that does the work keeps the important logic (and troubleshooting) in one place, but it is private as to not confuse the caller as to which delegate to use. More…
Our Do() method is where most of the magic happens, although it is only 60 lines with generous comments. Sharp observers will notice a couple odd things: it is private, and it takes two different delegate parameters. There are several overloads, these in turn call this method passing one or the other delegate, depending on whether we need a return value. This cleans the caller’s code greatly.
Towards cleaner code, a C# Asynchronous Helper (Pt 1 of 3)
Posted by Nicholas Brookins on 17 September, 2008 - 2 responses// Series Introduction
I’m a sucker for clean and beautifully groomed code. In my experience maintaining a large code base with few resources, it is in fact the only way to keep sanity and keep from sliding into a pit of spaghetti. I’ve been on a push to find areas of necessarily messy or repeated code and find ways to make them more elegant and maintainable - this is the first in a series* of small classes and helpers that I kick myself for not doing sooner.
UPDATE: This post is now an article on CodeProject.
// The Problem at Hand
In any reasonably large application there are a lot of times when you need to perform a quick task asynchronously. One use off-hand is application that saves a log or status report to file on start-up. The operation is slow in relative terms since it involves file I/O, and we don’t have to worry much about other code paths depending on it. Making it asynchronous will speed up our start time with no risk of side effects. More…

Subscribe to Posts
Subscribe by Email