Starting a Thread targeting method with parameters

Lets say you want to start a thread. How would you do it?
Thread T1 = new Thread(new ParameterizedThreadStart(SomeMethodName));

Restriction imposed by .NET is that Method which T1 targets can have only one parameter of object type. Hence, above code will work only if method signature is:
public static void SomeMethodName(object o)
But what if I want to invoke a method like:
public static void xyz (int a, int b)  ???
Answer is to use delegates here and start thread as:
Thread T3 = new Thread(delegate() { xyz(3, 4); });
or
Thread T3 = new Thread(() => xyz(3, 4)); 

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP