Multi-threading in .NET

In .NET 2.0+, if you want to have something run on another thread, you have a number of choices:

  • The classical System.Threading.Thread class
  • The not-so-known System.Threading.ThreadPool
  • The System.ComponentModel.BackgroundWorker
  • Delegates invoked asynchronously

Let's see how we would do it in each case, and what would be the benefits. Let's suppose our thread method looks like this:

private void ThreadMethod(Object state)

{

}

Using System.Threading.Thread

You start a new System.Threading.Thread instance and pass it a pointer to the thread method wrapped in a System.Threading.ParameterizedThreadStart (for an optional state parameter) or System.Threading.ThreadStart delegate, then, you start it, by calling Start:

Thread thread = new Thread(state => ThreadMethod(state));

thread.Start(state); 

As you can see, I am using the new .NET 3.5 lambda syntax for delegates.

If you want to wait for the thread to exit, do this:

thread.Join(timeout);

 

Using System.Threading.ThreadPool

Assuming your thread pool is properly configured (maximum number of threads, minimum number of threads before creating another one, etc), all you have to do is enqueue a method for execution, whenever a thread from the pool is free:

Boolean result = ThreadPool.QueueUserWorkItem(ThreadMethod, state);

Or, if you must wait for the result:

ManualResetEvent handle = new ManualResetEvent(false);

ThreadPool.RegisterWaitForSingleObject(handle, (s, timedout) => ThreadMethod(s), state, timeout, true);

handle.WaitOne();

By the way, QueueUserWorkItem always returns true, otherwise an exception is thrown.

 

Using System.ComponentModel.BackgroundWorker

You create an instance of the System.ComponentModel.BackgroundWorker class, add an event handler to its DoWork event, and start the processing by calling RunWorkerAsync, with an optional state parameter:

BackgroundWorker worker = new BackgroundWorker();worker.DoWork += delegate(Object sender, DoWorkEventArgs args) { ThreadMethod(args.Argument); };

worker.RunWorkerAsync(state); 

Waiting for the worker to complete is accomplished this way:

ManualResetEvent handle = new ManualResetEvent(false);

ThreadPool.RegisterWaitForSingleObject(handle, (s, timedout) => ThreadMethod(s), state, timeout, true);

handle.WaitOne();

The System.ComponentModel.BackgroundWorker class is available from the Windows Forms toolbox, in design view, so you can drag it into your form, and change its properties or register events through the property inspector.

 

Using Delegates

You declare a delegate that points to your method and you start its BeginInvoke method, passing it the state parameter and optionally a callback method, that gets called when the method terminates, and a state argument for that callback: Action<Object> action = ThreadMethod;

action.BeginInvoke(state, null, null);

And waiting:

Action<Object> action = ThreadMethod;IAsyncResult result = action.BeginInvoke(state, null, null);

result.AsyncWaitHandle.WaitOne(timeout);

 

So, what is the difference between all these methods? Let's see:

  • You would use System.Threading.Thread class when you want your task to be run exactly now; you may also want to suspend it or kill it;
  • System.Threading.ThreadPool helps preserving resources: no new threads are created, we just grab another one from the pool, if it is available, or wait for it, so the job may not start immediately; you don't have control over the actual thread that does the job;
  • If you want to have feedback from a thread, you use System.ComponentModel.BackgroundWorker. Its events allows you to set the completion status and to be notified when the task finishes. Typically you use it in a Windows Forms application. As it uses internally the thread pool, jobs may not start immediately;
  • Delegates are a fast way of launching a thread from a method delegate; it also uses the thread pool, so a task may take some time to actually start.

In all cases, starting from .NET 2.0, the spawned threads retain the same principal (System.Threading.Thread.CurrentPrincipal property), which is great for access control.

Bookmark and Share

                             

11 Comments

  • Such a glorious misuse of lambdas!


    -----------

    Thread thread = new Thread(state => ThreadMethod(state));
    thread.Start(state);

    As you can see, I am using the new .NET 3.5 lambda syntax for delegates.

  • Hey Everyone just intorducing myself here. Nice forum and glad i got the chance to join and hope to become an active member soon!

  • Hey there everyone i was just introduceing myself here im a first time visitor who hopes to become a daily reader!

  • Hello all
    I'm sorry - i'm inexperienced with computer. Im from Sirya!

    I need help about Anti-aging and HGH!
    I know that HGH injection is very good for antiaging, but I dont have much money for it...

    Is there any good and effective HGH or Anti-Aging supplement that works good and is not too expensive?
    I need to look young and beautiful for my Husband, please!

  • Hi all

    Christmas holidays are coming soon!
    please help me :
    What would be the good gift for guy for these holidays?

    I'm totally out of any ideas,tired thinking and i need help!

    Im 19F, he is 20

    Thanks all
    for your
    help and advice!

  • hi

    excuse me im new to internet and also
    very embarassed to ask about this...

    Anyone tried MaxVIGRA anti-ED supplement for men?
    I was adviced that MaxVIGRA works better and is cheaper than Vagra and such...

    Is that true? Please help me... i m shy to go to a doctor

  • whatsup this forum rocks!

    just registered and wanted to say hello

    i am 22 years old and from the uk and i am studying fashion

  • Hi,Hello everyone,

    I've got a question both to men and women -
    Anyone tried Goat weed or "Horny" goat weed ?
    Does it actually work and is it an effective libido enhancer both for women and men?

    Thanks in advance for your replies

  • Hey everyone, first post!

    What is your top five tips for better SEO on your website?

    Mine are:

    Accurate Title Tags
    Backlinks
    Keyword Rich Content
    Link Structure
    Relevancy

  • Hi,
    sorry, Im new to forums. My job is to complete survey about organic chinese green teas.
    If you have a minute, then could you please message me which is your favorite brand of green tea?
    Thanks in advance,
    Yours Jin

  • I enjoyed reading your blog. Keep it that way.

Comments have been disabled for this content.