Click here to Skip to main content
Licence CPOL
First Posted 26 Nov 2006
Views 33,584
Bookmarked 9 times

Sending parameters at thread startup

By | 26 Nov 2006 | Article
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:

ParameterizedThreadStart pts = 
          new ParameterizedThreadStart(MyParameterizedMethod);

My locale method needs the following signature:

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).

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.

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)

About the Author

Paul Eie


Bouvet ASA
Norway Norway

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhi PinmemberVishalSharmaDev22:38 14 Jan '07  
GeneralRe: hi PinmemberPaal Eie2:49 15 Jan '07  
GeneralRe: hi PinmemberVishalSharmaDev5:30 16 Jan '07  
GeneralRe: hi PinmemberPaal Eie0:09 17 Jan '07  
GeneralRe: hi PinmemberVishalSharmaDev7:11 24 Jan '07  
hi thanks for replying
 
i have solved out that problem in .net v1.1 ...
 
you can pass any no of parameter required in a thread to make it run accordingly..
 
solution is avaialble on demand...
 
thanks
vishal
 

 
Thanks & Regards
 
Vishal Sharma
vishalsharma556@yahoo.co.in

GeneralRe: hi Pinmemberbugsome20:26 18 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 27 Nov 2006
Article Copyright 2006 by Paul Eie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid