简体   繁体   English

System.Threading.Task不包含定义

[英]System.Threading.Task does not contain definition

I cant have .Delay definition on my System.Threading.Task. 我的System.Threading.Task上没有.Delay定义。

 public async Task<string> WaitAsynchronouslyAsync()
 {         
     await Task.Delay(10000);
     return "Finished";
 }

You are using .NET 4. Back then, there was no Task.Delay . 您正在使用.NET 4.那时,没有Task.Delay However, there was a library by Microsoft called Microsoft.Bcl.Async , and it provides an alternative: TaskEx.Delay . 但是,微软有一个名为Microsoft.Bcl.Async的库,它提供了一个替代方案: TaskEx.Delay

It would be used like this: 它会像这样使用:

public async Task<string> WaitAsynchronouslyAsync()
{         
    await TaskEx.Delay(10000);
    return "Finished";
}

Your other options are to either update to .NET 4.5, or just implement it yourself: 您的其他选择是更新到.NET 4.5,或者只是自己实现它:

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;
}

(taken from Servy's answer here ). (从Servy的答案采取在这里 )。

If I interpret the question correctly, it sounds like you are simply using .NET 4.0; 如果我正确地解释了这个问题,听起来你只是在使用.NET 4.0; Task.Delay was added in .NET 4.5. 在.NET 4.5中添加了Task.Delay You can either add your own implementation using something like a system timer callback and a TaskCompletionSource , or: just update to .NET 4.5 您可以使用系统计时器回调和TaskCompletionSource等添加自己的实现,或者:只需更新到.NET 4.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM