本文介绍了如何“睡觉”?直到在.NET 4.0中请求超时或取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最好的睡眠时间是什么,但是可以被 CancellationToken $ c的 IsCancellationRequested 打断$ c>?
What's the best way to sleep a certain amount of time, but be able to be interrupted by a IsCancellationRequested from a CancellationToken?
我正在寻找一种适用于.NET 4.0的解决方案。
I'm looking for a solution which works in .NET 4.0.
I' d想写
void MyFunc (CancellationToken ct) { //... // simulate some long lasting operation that should be cancelable Thread.Sleep(TimeSpan.FromMilliseconds(10000), ct); }推荐答案
我只是在这里写博客:
CancellationToken和Thread.Sleep
简而言之:
var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));在您的上下文中:
void MyFunc (CancellationToken ct) { //... // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }更多推荐
如何“睡觉”?直到在.NET 4.0中请求超时或取消
发布评论