I wish to provide creation options here, ie LongRunning, and possibly also a cancellation token
You can't customize TaskCreationOptions
on the Task
object returned by an async
method. Essentially, it doesn't make sense: you don't create the initial Task
here, Task.Delay
does. You can think of it like this:
Task Test()
{
var scheduler = SynchronizationContext.Current != null ?
TaskScheduler.FromCurrentSynchronizationContext() :
TaskScheduler.Current;
return Task.Delay(10).ContinueWith((t) => { },
CancellationToken.None,
TaskContinuationOptions.None, scheduler);
}
Now, that's possible to control TaskContinuationOptions
for the task returned by ContinueWith
(including TaskContinuationOptions.LongRunning
), but not TaskCreationOptions
.
You can however provide a cancellation token:
async Task Test(CancellationToken token)
{
await Task.Delay(10, token);
// ...
token.ThrowIfCancellationRequested();
}