Tag: cancellationtokensource

如何“睡眠”,直到在.NET 4.0中请求超时或取消

什么是睡眠一定时间的最佳方式,但能够被CancellationToken的IsCancellationRequested中断? 我正在寻找一个在.NET 4.0中工作的解决scheme。 我想写 void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable Thread.Sleep(TimeSpan.FromMilliseconds(10000), ct); }