System.Threading.Task不包含定义 [英] System.Threading.Task does not contain definition
本文介绍了System.Threading.Task不包含定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在System.Threading.Task上没有.Delay定义.
I cant have .Delay definition on my System.Threading.Task.
public async Task<string> WaitAsynchronouslyAsync()
{
await Task.Delay(10000);
return "Finished";
}
推荐答案
您正在使用.NET4.那时,还没有Task.Delay
.但是,有一个图书馆
微软称 Microsoft.Bcl.Async ,它提供了另一种选择:TaskEx.Delay
.
You are using .NET 4. Back then, there was no Task.Delay
. However, there was a library by
Microsoft called Microsoft.Bcl.Async, and it provides an alternative: TaskEx.Delay
.
它会这样使用:
public async Task<string> WaitAsynchronouslyAsync()
{
await TaskEx.Delay(10000);
return "Finished";
}
您的其他选择是更新到.NET 4.5,还是自己实施:
Your other options are to either update to .NET 4.5, or just implement it yourself:
public static Task Delay(double milliseconds)
{
var tcs = new TaskCompletionSource<bool>();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (o, e) => tcs.TrySetResult(true);
timer.Interval = milliseconds;
timer.AutoReset = false;
timer.Start();
return tcs.Task;
}
(摘自Servy的回答此处).
(taken from Servy's answer here).
这篇关于System.Threading.Task不包含定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文