异步方法在控制台应用程序中效果不佳
Asynchronous method does not work well in Console application
我正在使用下面的代码,想测试并行编程,但是有一些问题。
using System;
using System.Threading.Tasks;
namespace DemoParallelApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** Main Method Start ***");
Operation();
Console.WriteLine("*** Main Method End ***");
}
static void Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = Delay(5000);
Console.WriteLine("*** Operation Method End ***");
}
static async Task<int> Delay(int ms)
{
Console.WriteLine($"Start {ms}(ms) Delay");
await Task.Delay(ms);
Console.WriteLine($"End {ms}(ms) Delay");
return ms;
}
}
}
结果是这样的:
*** Main Method Start ***
*** Operation Method Start
*** Start 5000(ms) Delay
*** Operation Method End ***
*** Main Method End ***
不过我觉得应该是这样的:
*** Main Method Start ***
*** Operation Method Start ***
Start 5000(ms) Delay
*** Operation Method End ***
*** Main Method End ***
End 5000(ms) Delay
有什么问题吗?
您需要等待延迟:
static async Task Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = await Delay(5000);
Console.WriteLine("*** Operation Method End ***");
}
并使用 await 调用 Operation,在您的情况下 main
方法不是 asyncable
您可以这样做:
Task.Run(() => Operation()).GetAwaiter().GetResult();
您的程序在 Wait
之前完成并终止(包括“等待”方法)。您需要在某处等待 Delay
返回的任务。要获得所需的输出,您可以这样做,例如(使用 C# 7.1 async Main
feature):
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("*** Main Method Start ***");
var t = Operation();
Console.WriteLine("*** Main Method End ***");
await t;
}
static Task Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = Delay(5000);
Console.WriteLine("*** Operation Method End ***");
return task1 ;
}
static async Task<int> Delay(int ms)
{
Console.WriteLine($"Start {ms}(ms) Delay");
await Task.Delay(ms);
Console.WriteLine($"End {ms}(ms) Delay");
return ms;
}
}
我正在使用下面的代码,想测试并行编程,但是有一些问题。
using System;
using System.Threading.Tasks;
namespace DemoParallelApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** Main Method Start ***");
Operation();
Console.WriteLine("*** Main Method End ***");
}
static void Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = Delay(5000);
Console.WriteLine("*** Operation Method End ***");
}
static async Task<int> Delay(int ms)
{
Console.WriteLine($"Start {ms}(ms) Delay");
await Task.Delay(ms);
Console.WriteLine($"End {ms}(ms) Delay");
return ms;
}
}
}
结果是这样的:
*** Main Method Start ***
*** Operation Method Start
*** Start 5000(ms) Delay
*** Operation Method End ***
*** Main Method End ***
不过我觉得应该是这样的:
*** Main Method Start ***
*** Operation Method Start ***
Start 5000(ms) Delay
*** Operation Method End ***
*** Main Method End ***
End 5000(ms) Delay
有什么问题吗?
您需要等待延迟:
static async Task Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = await Delay(5000);
Console.WriteLine("*** Operation Method End ***");
}
并使用 await 调用 Operation,在您的情况下 main
方法不是 asyncable
您可以这样做:
Task.Run(() => Operation()).GetAwaiter().GetResult();
您的程序在 Wait
之前完成并终止(包括“等待”方法)。您需要在某处等待 Delay
返回的任务。要获得所需的输出,您可以这样做,例如(使用 C# 7.1 async Main
feature):
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("*** Main Method Start ***");
var t = Operation();
Console.WriteLine("*** Main Method End ***");
await t;
}
static Task Operation()
{
Console.WriteLine("*** Operation Method Start ***");
var task1 = Delay(5000);
Console.WriteLine("*** Operation Method End ***");
return task1 ;
}
static async Task<int> Delay(int ms)
{
Console.WriteLine($"Start {ms}(ms) Delay");
await Task.Delay(ms);
Console.WriteLine($"End {ms}(ms) Delay");
return ms;
}
}