是否可以运行Task
或启动Thread
异步?我想做的简短例子:
public static void Main () {
// Code
Task.Run(() => {
// WebRequest that works asynchron to console
// I don't need to output results
});
// Code
// Console shouldn't close until Task finished
}
答案 0 :(得分:2)
您可以在Task.Wait()
:
Task.Run()
Task t = Task.Run(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Task is about to exit");
});
Console.WriteLine("Now waiting for the task to exit...");
t.Wait();