Waiting for async delegate

Sometimes you want to create a task that you want to start as async but you want to track its status at a later date. The problem is that a task created this way will appear to have completed, and you’ll never know if it faulted or not. One solution is use a Func and  Unwrap the resulting task.


t = Task.Factory.StartNew(new Func(async () =>

{

while (true)

{

await Task.Delay(1000);

throw new ArgumentNullException();

}

value = 1;

})).Unwrap();

Edit – Whilst the above does have its uses, it’s better to use;

t = Task.Run(async () =>

Task.Run accepts Func and does the unwrapping for you.

Leave a comment