Pass Parameters through ParameterizedThreadStart

本小妞迷上赌 提交于 2019-12-20 08:28:05

问题


I'm trying to pass parameters through the following:

Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));

Any idea how to do this? I'd appreciate some help


回答1:


lazyberezovsky has the right answer. I want to note that technically you can pass an arbitrary number of arguments using lambda expression due to variable capture:

var thread = new Thread(
       () => DoMethod(a, b, c));
thread.Start();

This is a handy way of calling methods that don't fit the ThreadStart or ParameterizedThreadStart delegate, but be careful that you can easily cause a data race if you change the arguments in the parent thread after passing them to the child thread's code.




回答2:


Use overloaded Thread.Start method, which accepts object (you can pass your custom type or array if you need several parameters):

Foo parameter = // get parameter value
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
thread.Start(parameter);

And in DoMethod simply cast argument to your parameter type:

private void DoMethod(object obj)
{
    Foo parameter = (Foo)obj;
    // ...    
}

BTW in .NET 4.0 and above you can use tasks (also be careful with race conditions):

Task.Factory.StartNew(() => DoMethod(a, b, c));



回答3:


Another way to archive what you want, is by returning a delegate within your function / method. Take the following example:

class App
{
  public static void Main()
  {
     Thread t = new Thread(DoWork(a, b));
     t.Start();
     if (t.IsAlive)
     {
         t.IsBackground = true;
     }
  }

  private static ThreadStart DoWork(int a, int b)
  {
      return () => { /*DoWork*/ var c = a + b; };
  }

}



回答4:


new Thread(() => { DoMethod(a, b, c); }).Start();

or

new Thread(() => DoMethod(a, b, c)).Start();



回答5:


Instead of creating a class to pass in multiple parameters as @user1958681 has done, you could use anonymous types, then just use the dynamic typing to extract your parameters.

class MainClass
{
    int A = 1;
    string B = "Test";

    Thread ActionThread = new Thread(new ParameterizedThreadStart(DoWork));    
    ActionThread.Start(new { A, B});
}

Then in DoWork

private static void DoWork(object parameters)
{
   dynamic d = parameters;

    int a = d.A;
    string b = d.B;
 }



回答6:


// Parameters to pass to ParameterizedThreadStart delegate
// - in this example, it's an Int32 and a String:
class MyParams
{
    public int A { get; set; }
    public string B { get; set; }

    // Constructor
    public MyParams(int someInt, string someString)
    {
        A = someInt;
        B = someString;
    }
}

class MainClass
{
    MyParams ap = new MyParams(10, "Hello!");
    Thread t = new Thread(new ParameterizedThreadStart(DoMethod));
    t.Start(ap); // Pass parameters when starting the thread
}


来源:https://stackoverflow.com/questions/13776846/pass-parameters-through-parameterizedthreadstart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!