且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中有与此Java代码等效的东西吗?

更新时间:2023-02-07 20:46:48

您可以使用

You can use a Task to achieve this:

public class ThreadTest {

  public static void Main(string[] args) 
  {
    Task task = new Task(() => ... // Code to run here);
    task.Start();
  }
}

正如@JonSkeet所指出的那样,如果不需要分开创建和计划,则可以使用:

As @JonSkeet points out, if you do not need to separate creation and scheduling you could use:

Task task = Task.Factory.StartNew(() => ... // Code to run here);

或者在.Net 4.5+中:

Or in .Net 4.5+:

Task task = Task.Run(() =>  ... // Code to run here);