HI
I have a method update.sendUpdate(TrackManager tm);
TrackManager is a class
how do i create a thread for methods with parameters
appreciate a reply
thanks
HI
I have a method update.sendUpdate(TrackManager tm);
TrackManager is a class
how do i create a thread for methods with parameters
appreciate a reply
thanks
Have a look at how delegate
works.
Thread
s 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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.