Please ask one question at a time.
Read the docs on Task.Run.
private async Task Whatever(CancellationToken cancellationToken = default)
{
await Task.Run(async () =>
{
await DoTask();
}, cancellationToken);
}
private static async Task<bool> DoTask()
{
await Task.Delay(0);
return true; // dummy result
}
And for the record, this is what you are attempting
- Task.Run "should simply be thought of as a quick way to use Task.Factory.StartNew without needing to specify a bunch of parameters. It’s a shortcut"
- Task.Run(someAction); is exactly equivalent to:
Code
private async Task Whatever()
{
await Task.Factory.StartNew
(DoTask()
, CancellationToken.None
, TaskCreationOptions.None
, TaskScheduler.FromCurrentSynchronizationContext()
);
}
public static Action DoTask()
{
throw new NotImplementedException();
}