Click here to Skip to main content
15,891,629 members
Articles / Programming Languages / C#
Article

Sending parameters at thread startup

Rate me:
Please Sign up or sign in to vote.
3.27/5 (8 votes)
26 Nov 2006CPOL1 min read 53.1K   276   9   7
A small article showing how to send parameters to a thread at startup.

Introduction

Programming threads can be a very complex task if you don't have a clear strategy from the first code segment written. Trying to solve thread synchronization issues "live" can take a lot of time, and much could be saved if the developer has a more paranoid attitude when programming threads. I will not try to teach advanced thread programming, but only show a simple usage of sending parameters at thread start. What I find missing in many articles is the intermediary levels in thread programming, between the most simple thread examples and the more complex subjects.

Using the code

Unfortunately, you are only allowed to send in one parameter to the method, and this one parameter has to be of type object, but apart from that, you can wrap whatever you want in this object, from general classes to your own objects. My code segment will send an ArrayList as the parameter. To setup a thread startup with a parameter, you need to use the ParameterizedThreadStart which is a delegate located in the System.Threading namespace.

The delegate is setup by the following code segment:

C#
ParameterizedThreadStart pts = 
          new ParameterizedThreadStart(MyParameterizedMethod);

My locale method needs the following signature:

C#
private void MyParameterizedMethod(Object o)
{
    ...     
}

So now I am ready to use my method and the delegate. I then initalize everything as normal, only that I now specify a parameter to the Start method of the Thread class (I don't need to explicitly cast the ArrayList object).

C#
ParameterizedThreadStart pts = 
     new ParameterizedThreadStart(MyParameterizedMethod);
Thread t = new Thread(pts);
ArrayList al = new ArrayList();
al.Add("Hei");
al.Add(2);
t.Start(al);

The same applies when using the ThreadPool to run background threads, which also is the recommendation from Microsoft. But here, we use a WaitCallback instead of the ParameterizedThreadStart.

C#
WaitCallback myCallback = new WaitCallback(MyParameterizedMethod);
if (!ThreadPool.QueueUserWorkItem(myCallback, al))
{
    throw new Exception("Not able to queue work item");
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Bouvet ASA
Norway Norway
Paal started as a software consultant in 2000, where he has been working with everything from embedded C to Java, C++, Uniface and C#.

http://www.iserialized.com

Comments and Discussions

 
GeneralMy vote of 4 Pin
EgyptianRobot30-Aug-12 4:42
EgyptianRobot30-Aug-12 4:42 
Generalhi Pin
VishalSharmaDev14-Jan-07 22:38
VishalSharmaDev14-Jan-07 22:38 
GeneralRe: hi Pin
Paul Eie15-Jan-07 2:49
Paul Eie15-Jan-07 2:49 
GeneralRe: hi Pin
VishalSharmaDev16-Jan-07 5:30
VishalSharmaDev16-Jan-07 5:30 
GeneralRe: hi Pin
Paul Eie17-Jan-07 0:09
Paul Eie17-Jan-07 0:09 
GeneralRe: hi Pin
VishalSharmaDev24-Jan-07 7:11
VishalSharmaDev24-Jan-07 7:11 
GeneralRe: hi Pin
bugsome18-Jun-07 20:26
bugsome18-Jun-07 20:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.