Skip to content

9 Examples of creating a fast and responsive UI with multi-threading

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.

VN:F [1.8.3_1051]
Rating: 0.0/5 (0 votes cast)
VN:F [1.8.3_1051]
Rating: 0 (from 0 votes)
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

7 Comments

  1. STInG

    Cool samples! But when you push the buttons to start new thread and close the app it crashes. Such a shame…

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 07-Feb-09 at 4:09 am | Permalink
  2. STInG

    Sorry, your sample works fine, but MSDN’s crashes.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 07-Feb-09 at 4:11 am | Permalink
  3. Graham O'Neale

    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.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 07-Feb-09 at 9:14 am | Permalink
  4. hey dude… whtz up???

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 25-Feb-09 at 10:08 pm | Permalink
  5. 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!

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 17-Aug-09 at 9:54 am | Permalink
  6. Graham O'Neale

    @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 =:)

    UA:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 17-Aug-09 at 4:43 pm | Permalink
  7. 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

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Sep-09 at 8:23 am | Permalink

4 Trackbacks/Pingbacks

  1. [...] 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 [...]

  2. DotNetShoutout on 03-Feb-09 at 11:05 am

    9 Examples of creating a fast and responsive UI with multi-threading « {Programming} & Life…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
  3. [...] 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/ [...]

  4. [...] 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

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...