3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by nmaillet
0

Threads only support two types of delegates, ones with no parameters, and ones with a single object parameter. You can use structs (or classes, depending on the circumstances) to pass in multiple parameters, like so:

public struct MyParams
{
    public string Name;
    public int Id;
}

...

public void RunThread(object obj)
{
    MyParams params = (MyParams)obj;
    ...
}

You could then call it like this:

Thread thread = new Thread(RunThread);
thread.Start(new MyParams("test", 1));

You can also use Anonymous Methods to do this:

public void RunThread(string name, int id)
{
    ...
}

And call it like this:

string name = "test";
int id = 1;
Thread thread = new Thread(delegate() { RunThread(name, id); });
thread.Start();

This uses captured variables, as discussed in the above link, under Remarks. Given that methods started by threads are generally specific to the thread, I wouldn't normally recommend the use of anonymous methods though.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.