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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s