等待异步TaskEx [英] Await async TaskEx
问题描述
什么是TaskEx
?在 http://www.i-programmer.info/programming/c/1514-async-await-and-the-ui-problem.html?start=1 或等待异步澄清一个>.我用
Task DoWork()
{
return Task.Run(() =>
{
for (int i = 0; i < 30; i++)
{
Thread.Sleep(1000 * 60 * 30);
}
});
}
示例使用此
Task DoWork()
{
return TaskEx.Run(() =>
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(500);
}
}
});
我称它为await DoWork();
如果仅使用Task
,则await
不返回任何内容,并且没有响应.如果我使用TaskEx
,它说它在上下文中不存在.
TaskEx
应该是类还是具有某种功能的东西?
拳头一做是我的错.
I call it like await DoWork();
If you use just Task
, await
returns nothing and there is no response. If I use TaskEx
it says it doesn't exist in context.
Should TaskEx
be a class or something with some sort of function?
Fists one Works it's my mistake.
推荐答案
TaskEx
只是一个额外的类,它最初在.NET 4.5发行之前随C#5的async/await扩展的CTP一起提供...现在是异步定位包(又称为
TaskEx
was just an extra class which initially shipped with the CTPs of the async/await extensions for C# 5 before .NET 4.5 shipped... and is now part of the Async Targeting Pack (aka the Microsoft.Bcl.Async
NuGet package) in case you want to use async/await but are targeting .NET 4.0 (which doesn't have some of the code required for it).
如果您使用的是.NET 4.5或更高版本,则只需使用Task.Run
即可. (您将不会使用定位包,因此不会有TaskEx
.)异步定位包无法将静态方法添加到现有的Task
类中,因此需要TaskEx
存在于全部.
If you're using .NET 4.5 or later, just use Task.Run
, which does the same thing. (You won't be using the targeting pack, so you won't have TaskEx
.) The async targeting pack can't add a static method to the existing Task
class, hence the need for TaskEx
existing at all.
这篇关于等待异步TaskEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!