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

Recommended Answers

All 2 Replies

Have a look at how delegate works.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.