学习地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task.run?view=net-6.0
这些示例显示异步任务在与主应用程序线程不同的线程上执行。
对该方法的调用 Wait 可确保任务在应用程序结束之前完成并显示其输出。
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
Console.WriteLine("Application thread ID: {0}",
Thread.CurrentThread.ManagedThreadId);
var t = Task.Run(() => { Console.WriteLine("Task thread ID: {0}",
Thread.CurrentThread.ManagedThreadId);
} );
t.Wait();
}
}
// The example displays the following output:
// Application thread ID: 1
// Task thread ID: 3