C# 带参数的 ThreadStart

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1195896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:34:59  来源:igfitidea点击:

ThreadStart with parameters

c#multithreading

提问by JL.

How do you start a thread with parameters in C#?

在 C# 中如何启动带参数的线程?

采纳答案by Erick

Yep :

是的 :

Thread t = new Thread (new ParameterizedThreadStart(myMethod));
t.Start (myParameterObject);

回答by JaredPar

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.

Thread 构造函数的 2 个重载之一是 ParameterizedThreadStart 委托,它允许您将单个参数传递给 start 方法。不幸的是,虽然它只允许一个参数,但它以不安全的方式这样做,因为它将它作为对象传递。我发现使用 lambda 表达式来捕获相关参数并以强类型方式传递它们要容易得多。

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) {
  ...
}

回答by huseyint

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"
    }
}

回答by Justin Niessner

回答by CMS

You could use a ParametrizedThreadStartdelegate:

您可以使用ParametrizedThreadStart委托:

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

回答by Guffa

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

ParameterizedThreadStart一个参数。您可以使用它来发送一个参数或包含多个属性的自定义类。

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.

另一种方法是将要作为实例成员启动的方法与要设置的参数的属性一起放在类中。创建类的实例,设置属性并启动指定实例和方法的线程,方法可以访问这些属性。

回答by Spencer Ruport

Thread thread = new Thread(Work);
thread.Start(Parameter);

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

The parameter type must be an object.

参数类型必须是对象。

EDIT:

编辑:

While this answer isn't incorrect I do recommend against this approach. Using a lambda expression is much easier to read and doesn't require type casting. See here: https://stackoverflow.com/a/1195915/52551

虽然这个答案并没有错,但我确实建议不要使用这种方法。使用 lambda 表达式更容易阅读并且不需要类型转换。见这里:https: //stackoverflow.com/a/1195915/52551

回答by SwDevMan81

You can use the BackgroundWorkerRunWorkerAsyncmethod and pass in your value.

您可以使用BackgroundWorker RunWorkerAsync方法并传入您的值。

回答by Georgi-it

You can use lambda expressions

您可以使用 lambda 表达式

private void MyMethod(string param1,int param2)
{
  //do stuff
}
Thread myNewThread = new Thread(() => MyMethod("param1",5));
myNewThread.Start();

this is so far the best answer i could find, it's fast and easy.

这是迄今为止我能找到的最佳答案,它既快速又简单。