I\'m trying to pass parameters through the following:
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
Any idea how to do th
// Parameters to pass to ParameterizedThreadStart delegate
// - in this example, it's an Int32 and a String:
class MyParams
{
public int A { get; set; }
public string B { get; set; }
// Constructor
public MyParams(int someInt, string someString)
{
A = someInt;
B = someString;
}
}
class MainClass
{
MyParams ap = new MyParams(10, "Hello!");
Thread t = new Thread(new ParameterizedThreadStart(DoMethod));
t.Start(ap); // Pass parameters when starting the thread
}
Use overloaded Thread.Start method, which accepts object (you can pass your custom type or array if you need several parameters):
Foo parameter = // get parameter value
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
thread.Start(parameter);
And in DoMethod
simply cast argument to your parameter type:
private void DoMethod(object obj)
{
Foo parameter = (Foo)obj;
// ...
}
BTW in .NET 4.0 and above you can use tasks (also be careful with race conditions):
Task.Factory.StartNew(() => DoMethod(a, b, c));
class Program
{
public static void Main()
{
MyClass myClass = new MyClass();
ParameterizedThreadStart pts = myClass.DoMethod;
Thread thread1 = new Thread(pts);
thread1.Start(20); // Pass the parameter
Console.Read();
}
}
class MyClass
{
private int Countdown { get; set; }
public void DoMethod(object countdown) // Parameter must be an object and method must be void
{
Countdown = (int) countdown;
for (int i = Countdown; i > 0; i--)
{
Console.WriteLine("{0}", i);
}
Console.WriteLine("Finished!");
}
}
Instead of creating a class to pass in multiple parameters as @user1958681 has done, you could use anonymous types, then just use the dynamic typing to extract your parameters.
class MainClass
{
int A = 1;
string B = "Test";
Thread ActionThread = new Thread(new ParameterizedThreadStart(DoWork));
ActionThread.Start(new { A, B});
}
Then in DoWork
private static void DoWork(object parameters)
{
dynamic d = parameters;
int a = d.A;
string b = d.B;
}
Another way to archive what you want, is by returning a delegate within your function / method. Take the following example:
class App
{
public static void Main()
{
Thread t = new Thread(DoWork(a, b));
t.Start();
if (t.IsAlive)
{
t.IsBackground = true;
}
}
private static ThreadStart DoWork(int a, int b)
{
return () => { /*DoWork*/ var c = a + b; };
}
}
lazyberezovsky has the right answer. I want to note that technically you can pass an arbitrary number of arguments using lambda expression due to variable capture:
var thread = new Thread(
() => DoMethod(a, b, c));
thread.Start();
This is a handy way of calling methods that don't fit the ThreadStart
or ParameterizedThreadStart
delegate, but be careful that you can easily cause a data race if you change the arguments in the parent thread after passing them to the child thread's code.