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...
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...