cancellation of a Continous Task

  • Thread starter Thread starter JetSimon
  • Start date Start date
J

JetSimon

Guest
Hi:

I had this Exception whenever I cancel a continuous task.

"Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll"

then follow by "OperationCanceledException thrown with message: A task was canceled."

Is that acceptable ?

here are my test code:


private async void T(bool start,CancellationTokenSource tokensource)
{
var token = tokensource.Token;
var task= Task.Run(async () =>
{
while (start)
{

token.ThrowIfCancellationRequested();
DoWork();
if (token.IsCancellationRequested)
{
start = false;
token.ThrowIfCancellationRequested();
tokensource.Cancel();
}
Console.WriteLine("c {0}", hvalue[0].ToString());
await Task.Delay(100, tokensource.Token);





}

},tokenSource.Token);

try
{
await task;
}
catch (OperationCanceledException ex)
{
start = false;
Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {ex.Message}");
}
finally
{
tokenSource.Dispose();
}
}
private void DoWork()
{

counter += 1;



}

How can i handle this exceptioon (Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll) or just ignore it?


Thanks


Simon

Continue reading...
 

Similar threads

W
Replies
0
Views
99
Wally9633
W
N
Replies
0
Views
102
neonash
N
C
Replies
0
Views
88
Craig Muckleston (MCPD, MCTS)
C
M
Replies
0
Views
81
Markus Freitag
M
Back
Top