我通常使用 java 进行开发,但我现在必须使用 visual c# 2013 开发一个小的 wpf 应用程序并且......它似乎比 java 更复杂。 所以也许我会有很多关于它的问题。
目前我正在处理线程。 我的第一个问题是:这两种创建线程的方式有什么区别
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Threads_Button_Click_1(object sender, RoutedEventArgs e)
{
Thread th1 = new Thread(doSomething1);
th1.Start();
Thread th2 = new Thread(new ThreadStart(doSomething2));
th2.Start();
}
private void doSomething1()
{
Console.WriteLine("Starting doSomething1");
Thread.Sleep(3000);
Console.WriteLine("Finishing doSomething1");
}
private void doSomething2()
{
Console.WriteLine("Starting doSomething2");
Thread.Sleep(6000);
Console.WriteLine("Finishing doSomething2");
}
}
最佳答案
它们都是一样的,你看到的是语法糖 Delegate Inference .
Delegate inference allows you to make a direct assignment of a method name to a delegate variable, without wrapping it first with a delegate object.
简单来说,编译器发现 doSomething1
方法与 ThreadStart
委托(delegate)相匹配,因此只为您创建一个委托(delegate)。它知道它应该与 ThreadStart
匹配,因为它在构造函数的参数之一中。
有关更多可能性(使用 lambda 等...),请参阅 this blog .
关于c# - Visual C# .net 2013 和线程操作指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21187002/