Saturday, November 10, 2012

Best way to start a parameterized thread


//use this if you do not need a handle for the thread 
//(i.e. fire and forget)
public void Foo(string myString, int myInt)
{
   new Thread(() => Do_Foo(myString, myInt)).Start();      
}

//use this if you need a handle for the thread

public Thread Foo(string myString, int myInt)
{
   Thread t = new Thread(() => Do_Foo(myString, myInt));      
   t.Start();
   return t;
}


private void Do_Foo(string myString, int myInt)
{
   ...
   The stuff you want done
   ...
}

No comments:

Post a Comment