So I've this code:
//CancelationToken
CancellationTokenSource src = new CancellationTokenSource();
CancellationToken ct = src.Token;
ct.Register(() => Console.WriteLine("Abbruch des Tasks"));
//Task
Task t = new Task(() =>
{
System.Threading.Thread.Sleep(1000);
if (ct.IsCancellationRequested)
{
try
{
//Throw
ct.ThrowIfCancellationRequested();
}
catch (OperationCanceledException)
{
Console.WriteLine(
"ThrowIfCancellationRequested() liefert eben eine Exception");
}
}
}, ct);
//Run Task and Cancel
t.Start();
src.CancelAfter(350);
t.Wait();
// Get Information
Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
t.IsCanceled, t.IsCompleted, t.IsFaulted);
So in this case I canceled my Task but my output in the end is: "Canceled: False . Finished: True . Error: False"
In my opinion it should be "Canceled:True . Finished:False". Why do I get this result? Because I try to catch the exception?
I've tried it without the try - catch block, but then my program stops because of the OperationCanceledException. Can somebody help me?
When given a CancellationToken , you can create a new instance of the token source, assign it's token to the provided token, and cancel it. All other parties that can read this token will see that it's cancellation has been requested.
The associated CancellationToken will be notified of the cancellation and will transition to a state where IsCancellationRequested returns true. Any callbacks or cancelable operations registered with the CancellationToken will be executed.
You're swallowing the exception, thus the task is flagged as finished as you actually handle the exception and it doesn't propagate outwards.
Instead, don't catch the exception inside the delegate, catch it outside:
void Main()
{
CancellationTokenSource src = new CancellationTokenSource();
CancellationToken ct = src.Token;
ct.Register(() => Console.WriteLine("Abbruch des Tasks"));
Task t = Task.Run(() =>
{
System.Threading.Thread.Sleep(1000);
ct.ThrowIfCancellationRequested();
}, ct);
src.Cancel();
try
{
t.Wait();
}
catch (AggregateException e)
{
// Don't actually use an empty catch clause, this is
// for the sake of demonstration.
}
Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
t.IsCanceled, t.IsCompleted, t.IsFaulted);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With