When a managed thread is created, the method that executes on the thread is represented by a ThreadStart or ParameterizedThreadStart delegate that is passed to Thread constructor
For E.g.
To make it easy, Visual Basic and C# allow a short cut
Now the compiler works out which delegate constructor to use based on definition of MyMethod.
To pass a parameter, similpy pass a value when you call Start on the Thread
//definition of MyMethod
For E.g.
//to call a method with no parameters, use ThreadStart
Thread t1 = new Thread(new ThreadStart(MyMethod));
//to call a method with parametets, use ParameterizedThreadStart
Thread t1 = new Thread(new ParameterizedThreadStart(MyMethod));
To make it easy, Visual Basic and C# allow a short cut
//short cut method
Thread t1 = new Thread(MyMethod);
Now the compiler works out which delegate constructor to use based on definition of MyMethod.
To pass a parameter, similpy pass a value when you call Start on the Thread
t1.Start(14); ///will pass 14 to MyMethod
t1.Start("my message"); ///wiil pass "my message" to MyMethod
//definition of MyMethod
public static void MyMethod(object data)
{
//cast data to correct type and use data value
//for e.g. output as string
Console.WriteLine("Data {0}",data.ToString());
}
Comments