views:

5518

answers:

9

How is it possible to start a thread with parameters in C#

+8  A: 

Yep :

Thread t = new Thread (new ParameterizedThreadStart(myParamObject));
t.Start (myUrl);
Erick
is this the same: ThreadStart processTaskThread = delegate { ProcessTasks(databox.DataboxID ); }; new Thread(processTaskThread).Start();
JL
The `databox.DataboxID` will be encapsulated in the delegate you create (this is called a "closure"), so this is also a way to do it.
Thomas
Use ParameterizedThreadStart to be able to pass parameters to that delegate when you call Start method. You are using a ThreadStart delegate which does not expect a method with a parameter in its signature, that is wrong in your case.
huseyint
+19  A: 

One of the 2 overloads of the Thread constructor takse a ParameterizedThreadStart delegate which allows you to pass a single parameter to the start method. Unfortunately though it only allows for a single parameter and it does so in an unsafe way because it passes it as object. I find it's much easier to use a lambda expression to capture the relevant parameters and pass them in a strongly typed fashion.

Try the following

public Thread StartTheThread(SomeType param1, SomeOtherType param2) {
  var t = new Thread(() => RealStart(param1, param2));
  t.Start();
  return t;
}

private static void RealStart(SomeType param1, SomeOtherType param2) {
  ...
}
JaredPar
+1: Even though the currently selected answer is absolutely correct, this one by JaredPar is the better one. It simply is the best solution for most practical cases.
galaktor
This solution is much better then the standrard ParameterizedThreadStart
Piotr Owsiak
+1  A: 
class Program
{
    static void Main(string[] args)
    {
        Thread t = new Thread(new ParameterizedThreadStart(ThreadMethod));

        t.Start("My Parameter");
    }

    static void ThreadMethod(object parameter)
    {
        // parameter equals to "My Parameter"
    }
}
huseyint
+1  A: 

Use ParameterizedThreadStart.

Justin Niessner
This is scary :)
Thomas
+1  A: 

Use ParametrizedThreadStart.

Thomas
haha...good answer.
Justin Niessner
A: 

You could use a ParametrizedThreadStart delegate:

string parameter = "Hello world!";
Thread t = new Thread(new ParameterizedThreadStart(MyMethod));
t.Start(parameter);
CMS
A: 

The ParameterizedThreadStart takes one parameter. You can use that to send one parameter, or a custom class containing several properties.

Another method is to put the method that you want to start as an instance member in a class along with properties for the parameters that you want to set. Create an instance of the class, set the properties and start the thread specifying the instance and the method, and the method can access the properties.

Guffa
+2  A: 
Thread thread = new Thread(Work);
thread.Start(Parameter);

private void Work(object param)
{
    string Parameter = (string)param;
}

The parameter type must be an object.

Spencer Ruport
A: 

You can use the BackgroundWorker RunWorkerAsync method and pass in your value.

SwDevMan81