Swapnil Gupta Swapnil Gupta - 1 year ago 75
C# Question

How to pass parameters to ThreadStart method in Thread?

How to pass parameters to

method in C#?

Suppose I have method called 'download'

public void download(string filename)
// download code

Now I have created one thread in the main method:

Thread thread = new Thread(new ThreadStart(download(filename));

error method type expected.

How can I pass parameters to
with target method with parameters?

Answer Source

The simplest is just

string filename = ...
Thread thread = new Thread(() => download(filename));

The advantage(s) of this (over ParameterizedThreadStart) is that you can pass multiple parameters, and you get compile-time checking without needing to cast from object all the time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download