Passing data to Thread

Passing data to thread

we can pass data to the thread with the help of ParameterizedThreadStart delegate
This delegate specifies method with single parameter of object type. Code snippet below explains this in detail.

//Method with a object as a parameter
static void Display(object objMessage)
//typecast object to string
string msgtodisplay = (string) objMessage;
for(int i = 0; i<2;i++)
//Display message from the parameter
Console.WriteLine("{0}-> {1}", msgtodisplay,Thread.CurrentThread.ManagedThreadId);

static void Main(string[] args)
//Create new parameterised thread start delegate
ParameterizedThreadStart testParamThreadStart =
new ParameterizedThreadStart(Display);
//Create first thread
Thread myThread1 = new Thread(testParamThreadStart);
//Start thread and pass parameter
myThread1.Start("This is First Thread with Id :");
//Create second thread
Thread myThread2 = new Thread(testParamThreadStart);
//Start secodn thread and pass parameter to this thread
myThread2.Start("This is Second Thread with Id :");



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: