Passing in a string to a thread I am about to start? [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I am trying to accomplish something like this:

Code:
{
   string sType ="Update";
   Thread th1 = new Thread (new ThreadStart(Operation1(sType)));
   th1.Start();
}

private void Operaton1(string sType)
{
   if (sType == "Update")
   {
   }
   else
   {
   }
}

Obviouslly this doesn't work and returns and error message as follows:
Method name expected

It works fine if my "Operation1" has no parameters but I somehow need to pass the information from "sType" into the Thread itself...
How could I accomplish passing a STRING into a THREAD I am about to start?

Any help would be greatly appreciated.
Thanks,
 
I found one answer to this question in the intellisense that popped up when I typed new Thread(. Assuming it won't be a problem to change the signature of Operation1, try this:

C# Code[HorizontalRule]magic hidden text[/HorizontalRule]{
···string sType ="Update";
···// Create a thread that accepts a parameter
···Thread th1 = new Thread (
······new ParameterizedThreadStart(Operation1));
···// Execute thread with specified parameter
···th1.Start(stype);
}

// method signature has been changed
private void Operaton1(object oType)
{
···// String comparison has been modified
···if ("Update".Equals(oType))
···{
···}
···else
···{
···}
}
[HorizontalRule]Why are you quoting me?[/HorizontalRule]

Also note that the object passed to the thread doesn't have to be a string--it could be an object that specified any kind of data you'd like. You could even tailor a class that the invoked thread can write data back to in order to return data from a thread.
 
Back
Top