【发布时间】:2012-10-01 21:29:08
【问题描述】:
我的代码必须定期轮询外部资源。简化后看起来像:
CancellationTokenSource cancellationSource = new CancellationTokenSource();
Task t = Task.Factory.StartNew(() =>
{
while (!cancellationSource.IsCancellationRequested)
{
Console.WriteLine("Fairly complex polling logic here.");
// Finishes sleeping before checking for cancellation request
Thread.Sleep(10000);
}
},
cancellationSource.Token);
如何以这样一种方式对 10 秒的延迟进行编码,以便在调用 cancelSource.Cancel() 时将其中断?
【问题讨论】:
标签: .net multithreading