.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

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: 

http://www.audacs.co.uk/ViewPage.aspx?PageID=474


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.