嗨,我一直在论坛上阅读了很多,但我无法找到我的问题的答案......
这是我想要在布尔值变为 TRUE 时取消的函数:
Task<PortalODataContext> task = Task.Factory.StartNew(() =>
{
var context = connection.ConnectToPortal();
connection.ListTemplateLib = this.ShellModel.ConnectionManager.GetTemplateLibrarys(connection);
connection.ListTemplateGrp = this.ShellModel.ConnectionManager.GetTemplateGroups(connection, connection.TemplateLibraryId);
connection.ListTemplates = this.ShellModel.ConnectionManager.GetTemplates(connection, connection.TemplateGroupId);
return context;
}, token);
如何验证令牌是否在没有 LOOP 的情况下收到取消请求?
类似的东西:
if (token.IsCancellationRequested)
{
Console.WriteLine("Cancelled before long running task started");
return;
}
for (int i = 0; i <= 100; i++)
{
//My operation
if (token.IsCancellationRequested)
{
Console.WriteLine("Cancelled");
break;
}
}
但我没有需要循环的操作,所以我不知道该怎么做......