以下两个代码片段是否执行相同的操作?
//--------------------------------------------------
1.
//--------------------------------------------------
var producer = Task.Run(async () =>
{
await bar.ReadDataAsync();
});
var consumer = Task.Run(async () =>
{
await bar.WriteDataAsync();
});
await Task.WhenAll(consumer, producer);
//--------------------------------------------------
2.
//--------------------------------------------------
await Task.WhenAll(bar.ReadDataAsync(), bar.WriteDataAsync());
ReadDataAsync
和WriteDataAsync
返回的是Task
(而不仅仅是可等待对象),你不需要将它们包装在Task.Run()
中。在大多数情况下,var producer = bar.ReadDataAsync()
就足够了,并且更有意义。 - shay__