Task does not complete if another task with in it gets cancelled - c#

In this code, the task t will never complete (never outputs to the console) if I cancel the token, even though the token is not used for t and only used for a task inside t
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
Task t = Task.Factory.StartNew(async () =>
{
await Task.Delay(1000, token);
Console.WriteLine("Task Completed");
});
source.Cancel();
How can I cancel the token but also let the task complete
Notes:
I tried using Task.Run instead, and got the same output
t.IsCompleted will stay false indefinitely (task will never complete)
The delay task does complete

You can make the t to survive the cancellation, by handling and suppressing the OperationCanceledException that is thrown by the Task.Delay:
Task t = Task.Run(async () =>
{
try { await Task.Delay(1000, token); } catch (OperationCanceledException) { }
Console.WriteLine("Task Completed");
});

Related

Is there a way to force a context switch without Task.Delay in async code

I have code with looks something like:
I was trying to avoid putting it in a Task.Run because it is async and should run fine on the main thread
However it does not do a context switch (and will run forever), unless I insert a Task.Delay into the loop
Is there a better way of achieving this (Without Task.Run)?
var tasks = new List<Task>();
var cts = new CancellationTokenSource();
tasks.Add(DoSomething(cts.Token));
cts.Cancel();
Task.WaitAll(tasks.ToArray());
Console.WriteLine("Done");
async Task DoSomething(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
await Task.CompletedTask;
await Task.Delay(1); // Without this is doesn't do context switch
}
}
I'm trying to unit test cancellation when I have a heavy workload an encountered this problem.
Try
async Task DoSomething(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
//await Task.CompletedTask; //you can omit this line
await Task.Yield();
}
}
If you want to simulate a cancelable operation, the simplest way is probably this:
Task DoSomething(CancellationToken cancellationToken)
{
return Task.Delay(Timeout.Infinite, cancellationToken);
}
The cancellationToken parameter has canceling semantics, meaning that when the token is canceled, the Task will transition to the Canceled state. If you want it to have stopping semantics, which is the non standard semantics for a CancellationToken, you could do this:
async Task DoSomething(CancellationToken stoppingToken)
{
try { await Task.Delay(Timeout.Infinite, stoppingToken); }
catch (OperationCanceledException) { }
}
Now when the token is canceled, the Task will transition to the RanToCompletion state. Be aware that it's extremely rare to see a CancellationToken parameter with stopping semantics in the standard .NET libraries.

Timeout and stop a Task

I have a console app,
{
StartThread();
//must be true, windows system wants to know it is started
return true;
}
I'm trying to create a safety timeout function for this Task. But the task keeps running...
The method DoSomething calls other async methods and awaits their result
Do anyone have an idea why my task don't stop? Maybe a good code example on what to do
public async void StartThread()
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
try
{
var timeout = 1000;
Task task = new Task(() => DoSomething(token), token);
task.Start();
if (await Task.WhenAny(task, Task.Delay(timeout, token)) == task)
{
if (token.IsCancellationRequested)
task.Dispose();
await task;
if (task.IsCompleted)
{
task.Dispose();
tokenSource.Cancel();
tokenSource.Dispose();
}
else
{
log.WriteToFile("Timeout_ ");
}
}
else
tokenSource.Cancel();
}
catch (Exception e)
{
Console.WriteLine("--StartThread ...there is an exception----");
}
finally
{
Thread.Sleep(300000); // 5 minutter
StartThread();
}
}
While not create CancellationTokenSource from given timeout?
var timeout = 1000;
//DONE: don't forget to dispose CancellationTokenSource instance
using (var tokenSource = new CancellationTokenSource(timeout)) {
try {
var token = tokenSource.Token;
//TODO: May be you'll want to add .ConfigureAwait(false);
Task task = Task.Run(() => DoSomething(token), token);
await task;
// Completed
}
catch (TaskCanceledException) {
// Cancelled due to timeout
log.WriteToFile("Timeout_ ");
}
catch (Exception e) {
// Failed to complete due to e exception
Console.WriteLine("--StartThread ...there is an exception----");
//DONE: let's be nice and don't swallow the exception
throw;
}
}
You should hardly ever Dispose a Task, since iot is managed by C# internals and it is taken care of. Also, you Dispose way too eagerly, for example:
if (token.IsCancellationRequested)
task.Dispose();
await task;
I do not think so you want still await task if it cancelled and disposed. I guess it will not work at all.
Also if you use async, do not mix blocking calls such as Thread.Sleep - that can lead to disaster...
After all, you use cancellation token with some delay task to imitate a timeout - that's OK, but why do put unnecessary code, when you have great API at hand. Just make use of special contructor of CancellationTokenSource:
public CancellationTokenSource (int millisecondsDelay);
Here's the docs
After a timeout you are setting the CancellationToken and then immediately sleeping the thread for 5 minutes. Thus DoSomething() never gets a chance to continue running and react to the token being cancelled.

Set timeout for user defined function using cancelation token

I want to cancel a Task if it lasts longer than 3 seconds.
First attempt:
public static async Task DoSomething()
{
await Task.Delay(10000);
}
var task1 = DoSomething();
var task2 = Task.Delay(3000);
Task.WaitAny(task1, task2);
Second attempt:
I tried to use cancellationToken but it does not seems to work. It waits for the function 10 seconds and it seems it ignores the 3 seconds delayed cancellation.
var cts = new CancellationTokenSource();
var token = cts.Token;
cts.CancelAfter(TimeSpan.FromSeconds(3));
await Task.Run(async () => await DoSomething(), token);
Can someone help me with implementing such functionality using cancellationToken?
You need to pass CancellationToken to DoSomething and use it there:
public static async Task DoSomething(CancellationToken t)
{
await Task.Delay(10000, t);
}
var cts = new CancellationTokenSource();
var token = cts.Token;
cts.CancelAfter(TimeSpan.FromSeconds(3));
try
{
await Task.Run(async () => await DoSomething(token), token);
}
catch (OperationCanceledException ex)
{
// canceled
}
From docs:
When a task instance observes an OperationCanceledException thrown by user code, it compares the exception's token to its associated token (the one that was passed to the API that created the Task). If they are the same and the token's IsCancellationRequested property returns true, the task interprets this as acknowledging cancellation and transitions to the Canceled state. If you do not use a Wait or WaitAll method to wait for the task, then the task just sets its status to Canceled.
But it seems Task transitions to Canceled independent of if the same token is provided or not.
The only difference I could find between passing token to Task.Run or not was that if cancellation is requested before the task begins execution, the task does not execute, instead it is set to the Canceled state and throws a TaskCanceledException exception.

Cancel Task and set status to "Cancelled"

I want a task to finish when the 50 Milliseconds are over. The status of the task should then be set to "Cancelled", otherwise to "RunToCompletion".
The task creation is here:
CancellationTokenSource cts = new CancellationTokenSource(50);
CancellationToken ct = cts.Token;
Task test_task = Task.Run(async () =>
{
try
{
tokenS.Token.Register(() =>
{
cts.Cancel();
ct.ThrowIfCancellationRequested();
});
await NotifyDevice(BLEDevice);
}
catch (Exception e)
{
}
},ct);
All i get till now is an AggregateException, that does not get catched somehow by the try/catch-block.
Here is similar question to yours: Is it possible to cancel a C# Task without a CancellationToken?. But the solution will not cancel the task in NotifyDevice method. That task can be cancelled only if underlying task supports cancellation. And based on the docs I​Async​Info can be cancelled. I would go with the wrapper to ensure the task is cancelled in 50ms in case if cancelling underlying task takes more time:
CancellationTokenSource cts = new CancellationTokenSource(50);
await NotifyDevice(BLEDevice, cts.Token).WithCancellation(cts.Token);
EDIT: the extension method itself:
public static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using(cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
if (task != await Task.WhenAny(task, tcs.Task))
throw new OperationCanceledException(cancellationToken);
return await task;
}

Why is this Task not Finishing before my code passes the Wait Command

I have a task which runs a loop and delays for an interval each iteration. Once the CancellationTokenSource calls Cancel() I want my main code to Wait() for the Task.Delay(interval) to finish and the task to exit the loop before my code continues. I thought this code would work but it doesn't.
Instead my main code passes the t.Wait() before the Loop exits. Why?
Main Method Code:
var cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
var t = Task.Run(() => { MyLoopTask(200, ct); });
// Prepare information
cts.Cancel();
t.Wait();
// Send Information
Task Code
private async Task MyLoopTask(int interval, CancellationToken cancelToken)
{
while (!cancelToken.IsCancellationRequested)
{
Debug.Print(" Still In Loop ");
// Do something
await Task.Delay(interval);
}
Debug.Print(" cancelled ");
//Clean up
}
You create a task with Task.Run that fires and forgets the actual task you get back from MyLoopTask.
Task.Run is redundant here. You can just call MyLoopTask and use the task it returns.
var t = MyLoopTask(200, ct);
// ...
t.Wait();
If you still have some reason to use Task.Run you can do so by making sure the delegate waits for the actual task by awaiting it:
var t = Task.Run(async () => await MyLoopTask(200, ct));
// ...
t.Wait();

Categories

Resources