Finally, a superb article on multi-threading for Windows application programming (what am I talking about finally, it’s probably been there for years I just my mind usually immediately demotes the value of an msdn article as being uninformative and vague) but this one was actually pretty good and delves into common case scenarios with multi-threaded application programming, and discusses the tenets and pitfalls you may run into it. It also explains in great detail how to gear up your user interface to be responsive and maximum allowable time for work before it should be shipping off to external worker threads.
I’m sure we’ve all had experience, or no doubt heard of multi-threaded applications but creating them, at least I had found always to be a grey area. I end up implementing a different approach every time I do it.
So that leads me to say, I have created seven nine delightful threading samples wrapped up into a .NET 3.5 app for everyone to enjoy, for me this is my definitive “go-to” code now whenever I need to tackle threading, hopefully it will be yours too.
In the header of my notes I also state which one out of the nine I use when implementing multi-threading.

Download ‘9 Multi Threading Samples‘ here.
Note: Source code re-worked on 02/02/2009 to include further clearer, and fat-trimmed examples.
7 Comments
Cool samples! But when you push the buttons to start new thread and close the app it crashes. Such a shame…
Sorry, your sample works fine, but MSDN’s crashes.
Thanks. glad you like them.
Yes I mentioned in code commenting I was experiencing a crash if you close the app whilst a thread is running, however luckily it is not due to the reading code, but rather a windows forms exception. However I imagine terminating your thread cleanly prior to the app closing, as msdn states would fix this issue.
hey dude… whtz up???
From line 277 of Form1.cs
// TODO: No idea why we need ‘new object[] {list}’ and not just ‘list’, after all list is of type object[]
// If anyone could shed some light on the matter, please feel free to do so on my blog.
Thought I’d input as I’ve been looking through your code. The reason you need a new object[] is because the parameter to your method is an object[] and so you really need an array with the array as the first element. Otherwise the compiler is trying to interpret your object array as individual parameters to the method you are trying to invoke.
At least that’s what it looks like to me.
BTW, I’ve held onto this code for a while in order to have easy access to all the methods for asynchronous invocation for UI updating. Thanks!
@Mike cool, thanks!
By the way, if you’re using .NET 3.0 I think or later, probably the simplest declaration to perform a command on the UI thread is this:
this.BeginInvoke((MethodInvoker) delegate { this.buttonLogin.Text = "Logging In..."; this.buttonLogin.Enabled = false; });Where “this” is your form and the closure contains whatever UI elements you want to change.
Somewhat streamlined again since the day I did that sample =:)
I’m so glad I found this site…Keep up the good work I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog. Thanks,
A definite great read.. <a href="http://wiki.hudson-ci.org/display/~bill-bartmann"
-Bill-Bartmann
4 Trackbacks/Pingbacks
[...] the new source code on my updated previous article here: http://goneale.wordpress.com/2009/01/27/7-examples-of-creating-a-fast-and-responsive-ui-with-multi-t... « Unexpected [nasty!!!!] problem utilising [Serializable] & [DataContract] together [...]
9 Examples of creating a fast and responsive UI with multi-threading « {Programming} & Life…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] 9 Examples of creating a fast and responsive UI with multi-threading – http://goneale.wordpress.com/2009/01/27/9-examples-of-creating-a-fast-and-responsive-ui-with-multi-threading/ [...]
[...] with multi-threading in my app, you would think I would be good at this by now considering I blogged about it not too long ago acting like I knew what I was talking [...]
Post a Comment