Author - Dave Amour

Dave Amour has used computers for as long as he can remember and intially started out as an IT trainer delivering a range of IT courses but for the last 11 years has been focusing on the arena of web application development. He was worked for numerous companies over the years and is currently working for Audacs Software Ltd. Dave is also a keen squash player and an active and sucessful member of Experts Exchange

Please feel free to submit any constructive comments which you can do at the bottom of this page.

Dave may be available for programming tuition or consultancy work. Contact via dave@audacs.co.uk
Dave Amour - Click to view CV 

Threading - ThreadStart versus ParameterizedThreadStart

When we want to create a thread in a .net application we would normally do so by folling these steps.

First include a using statement for the Threading namespace as follows:

using System.Threading;


We then create a new instance of a thread using the following syntax:

Thread thread = new Thread(new ThreadStart(DoSomeWork));


Where in this case the ThreadStart is a delegate which will hold a reference to the DoSomeWork method.  If you are unsure of delegates then have a read of the following article first: 


The entire DoSomeWork method will look like this:

        static void DoSomeWork()
        {
            Console.WriteLine("I am doing some work");
        }


Our entire test console app will then look like this.  Note also the call to thread.Start() to start the thread.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace ThreadingTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(DoSomeWork));
 
            thread.Start();
 
            Console.Read();
        }
 
        static void DoSomeWork()
        {
            Console.WriteLine("I am doing some work");
        }
    }
}
 

The output of the above code should then look like this:
 


So this all works very well but what if we want to pass in some arguments to our DoSomeWork method?  Well there are two ways of achieving this.  Firstly we could create an instance of an object which has the method we are going to run and populate some instance fields prior to passing in the method name to the ThreadStart constructor.  If that doesn't quiet make sense then have a look at  the following code.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace ThreadingTest2
{
    class Program
    {
        static void Main(string[] args)
        {
            Person dave = new Person();
            Person sarah = new Person();
 
            dave.FirstName = "Dave";
            dave.Surname = "Amour";
 
            sarah.FirstName = "Sarah";
            sarah.Surname = "Williams";
 
            Thread daveThread = new Thread(new ThreadStart(dave.SayName));
            Thread sarahThread = new Thread(new ThreadStart(sarah.SayName));
 
            daveThread.Start();
            sarahThread.Start();
 
            Console.Read();
        }
 
        static void DoSomeWork()
        {
            Console.WriteLine("I am doing some work");
        }
    }
 
    class Person
    {
        public string FirstName
        {
            get;
            set;
        }
 
        public string Surname
        {
            get;
            set;
        }
 
        public void SayName()
        {
            Console.WriteLine("My name is {0} {1}", FirstName, Surname);
        }
    }
}


This should yield the following results:



So this is clever but really quite simple way of achieving parameterised threading even though there are no actual parameters, we achieve the same kind of result.

The other way to achieve parameterised queries is to using a different delegate to the ThreadStart delegate.  There is another delegate we can use which is called ParameterizedThreadStart.  This delegate signature takes an object as a parameter and when we call the Start method on the thread, that is when we pass in our object argument.

Sample code for this might look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace ThreadingTest3
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(new ParameterizedThreadStart(DoSomeWork));
            Thread thread2 = new Thread(new ParameterizedThreadStart(DoSomeWork));
            Thread thread3 = new Thread(new ParameterizedThreadStart(DoSomeWork));
 
            thread1.Start("Wibble");
            thread2.Start("VooVarVoo");
            thread3.Start("Iranoo");
 
            Console.Read();
        }
 
        static void DoSomeWork(object testArg)
        {
            Console.WriteLine("I am doing some work with {0}", testArg);
        }
    }
}


Results for this code should be as follows:

So again this is quite a nice way of achieving the desired result.

It is flexible since we can pass in any object but may have to do some casting but otherwise it works well.

Any questions or comments welcome via the form below.

Leave a comment

Name

Email (optional and not disclosed)



Email address is not disclosed but just used to alert you of new posts if entered
Word CV
HTML CV
PDF CV
Text CV
CMS Lite
ETraining
Planet Health
Florida Health
the Date Shack
Taylors
Emcat
Emtex
Browne Jacobson
Alliance & Leicester
Baird Leisure
Swarfega
Creature Comforts
Rugeley Chess Club
Katnip
Katmaid
Dudley NHS
Contact Me
Current Status