C# 线程与线程启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29862234/
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
Thread vs Threadstart
提问by Shashank sarma
In C#, practically, I haven't observed any difference between the following:
实际上,在 C# 中,我没有观察到以下内容之间的任何区别:
new Thread(SomeMethod).Start();
,
,
new Thread(new ParameterizedThreadStart(SomeMethod));
and
和
new Thread(new ThreadStart(SomeMethod));
What is the difference, if there is any at all?
有什么区别,如果有的话?
采纳答案by Douglas
The Thread(ThreadStart)constructor can only be used when the signature of your SomeMethodmethod matches the ThreadStartdelegate. Conversely, Thread(ParameterizedThreadStart)requires SomeMethodto match the ParameterizedThreadStartdelegate. The signatures are below:
该Thread(ThreadStart)构造只能用来当你的签名SomeMethod方法相匹配ThreadStart的委托。相反,Thread(ParameterizedThreadStart)需要SomeMethod匹配ParameterizedThreadStart委托。签名如下:
public delegate void ThreadStart()
public delegate void ParameterizedThreadStart(Object obj)
Concretely, this means that you should use ThreadStartwhen your method does not take any parameters, and ParameterizedThreadStartwhen it takes a single Objectparameter. Threads created with the former should be started by calling Start(), whilst threads created with the latter should have their argument specified through Start(Object).
具体来说,这意味着你应该ThreadStart在你的方法不带任何参数时使用,ParameterizedThreadStart当它只带一个Object参数时。使用前者创建的线程应通过调用启动Start(),而使用后者创建的线程应通过 指定其参数Start(Object)。
public static void Main(string[] args)
{
var threadA = new Thread(new ThreadStart(ExecuteA));
threadA.Start();
var threadB = new Thread(new ParameterizedThreadStart(ExecuteB));
threadB.Start("abc");
threadA.Join();
threadB.Join();
}
private static void ExecuteA()
{
Console.WriteLine("Executing parameterless thread!");
}
private static void ExecuteB(Object obj)
{
Console.WriteLine($"Executing thread with parameter \"{obj}\"!");
}
Finally, you can call the Threadconstructors without specifying the ThreadStartor ParameterizedThreadStartdelegate. In this case, the compiler will match your method to the constructor overload based on its signature, performing the cast implicitly.
最后,您可以在Thread不指定ThreadStart或ParameterizedThreadStart委托的情况下调用构造函数。在这种情况下,编译器将根据其签名将您的方法与构造函数重载相匹配,隐式执行强制转换。
var threadA = new Thread(ExecuteA); // implicit cast to ThreadStart
threadA.Start();
var threadB = new Thread(ExecuteB); // implicit cast to ParameterizedThreadStart
threadB.Start("abc");
回答by idstam
No, but there are a lot of times when your code look a lot nicer if you create the ThreadStart object in one place and starts the new thread in another place.
不,但是很多时候如果您在一个地方创建 ThreadStart 对象并在另一个地方启动新线程,您的代码看起来会更好。
回答by stakx - no longer contributing
new Thread(SomeMethod)and new Thread(new ThreadStart(SomeMethod)):
new Thread(SomeMethod)和new Thread(new ThreadStart(SomeMethod)):
The difference between new Thread(SomeMethod)and new Thread(new ThreadStart(SomeMethod))is purely syntactical: The C# compiler generates the same code for these; the former version is an abbreviation of the latter.
new Thread(SomeMethod)和之间的区别new Thread(new ThreadStart(SomeMethod))纯粹是语法上的:C# 编译器为它们生成相同的代码;前者是后者的缩写。
(The compiler can automatically deduce the proper delegate type to use from the signatures of the available Threadconstructors, and the signature of the specified method SomeMethod. Writing out new ThreadStart(…)instead of just …is a little bit like replacing varwith the actual type of some expression, you're sparing the compiler the work of figuring out the actual type.)
(编译器可以从可用Thread构造函数的签名和指定方法的签名中自动推导出正确的委托类型SomeMethod。写出new ThreadStart(…)而不是仅仅…有点像用var某些表达式的实际类型替换,你是省去编译器找出实际类型的工作。)
These two versions work when SomeMethodtakes no parameters, because that's the signature required to match the ThreadStartdelegate.
这两个版本在SomeMethod不带参数时工作,因为这是匹配ThreadStart委托所需的签名。
new Thread(new ParameterizedThreadStart(SomeMethod)):
new Thread(new ParameterizedThreadStart(SomeMethod)):
The difference between the above two and new Thread(new ParameterizedThreadStart(SomeMethod))is that this one is calling a different constructor on Thread.
上述两者之间的区别在于new Thread(new ParameterizedThreadStart(SomeMethod)),这个是在 上调用不同的构造函数Thread。
And that ParameterizedThreadStartproscribes a different method signature than ThreadStart: Your SomeMethodneeds to take one argument of type object, otherwise it does not match this delegate type.
这ParameterizedThreadStart禁止了一种不同的方法签名ThreadStart:您SomeMethod需要采用一个 type 参数object,否则它与此委托类型不匹配。

