作为一个标题,我需要强制停止任务,我尝试过使用tokenSource但不幸的是没有工作...... :(
这是我的代码:
public Task StartAsync()
{
_tokenSource = new CancellationTokenSource();
_token = _tokenSource.Token;
return Task.Run(() =>
{
do
{
Reset();
try
{
DoWorkAsync().Wait();
}
catch (Exception e)
{
Dictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("descrizione", "Eccezione gestita esternamente!");
RegisterExceptionsAsync(e, properties).Wait();
}
PrintMatches();
RegisterSummaryAsync().Wait();
if (!_stop)
{
System.Threading.Thread.Sleep(_options.LapTimeout);
}
}
while (!_stop);
}, _token);
}
public void Stop()
{
_stop = true;
}
public void ForceStop()
{
_tokenSource.Cancel(false);
WatchTime.Stop();
}
当在Main函数中我调用ForceStop()方法时,任务没有停止但是在运行状态下是连续的......任何想法? 提前谢谢!