【发布时间】:2015-06-18 08:22:23
【问题描述】:
我知道如何取消超时执行的方法。我的意思是Task-Wait-Timeout-CancellationToken 把戏。 IE。方法包装在任务中。这种技术效果很好。例如:
private void TestConnectMethod()
{
int QueryTimeOut = 1000; //in ms
_txtError.Visibility = Visibility.Hidden;
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
var task = Task.Factory.StartNew(() =>
{
while (true)
{
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
Debug.WriteLine("Iteration" + DateTime.Now);
}
}, token).ContinueWith(t =>
{
});
var wres = task.Wait(QueryTimeOut);
if (!wres)
{
cancellationTokenSource.Cancel();
_txtError.Text = "Timeout!";
}
else
{
_txtError.Text = "All is ОК";
}
}
任务将被成功取消。但是如果 Task 看起来像这样:
var task = Task.Factory.StartNew(() =>
{
// This is a server method that can be suspended
ArchiveServiceClient.Instance.Authenticate()
}, token).ContinueWith(t =>
{
});
ArchiveServiceClient.Instance.Authenticate() 方法可以在服务器没有响应时暂停应用程序。现在,我不能写了
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
因为这些字符串将毫无用处。如何停止 Task 使用挂起方法执行?有可能吗?
【问题讨论】:
-
1) 如果您调用的方法不支持超时/取消,您无法优雅地停止它。 2)但是为什么这会暂停应用程序?它不应该简单地阻塞它正在运行的线程吗? 3)你为什么用
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();而不是token.ThrowIfCancellationRequested();? -
@CodesInChaos, 2) - 你说得对,它只会阻塞线程,但对我来说这也是不可接受的。 3)这里没关系,两种方法的工作方式相同。
标签: c# task cancellation