Hi I've been reading alot on the forum but I'm not able to find the answer to my problem...
Here's my function that I want to cancel when a boolean turn to 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);
How can I verify if the token got a cancel request without a LOOP ?
Something like that :
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;
}
}
But I dont have an operation that require a Loop so I dont know how to do that ...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…