C# .Net Framework Disposing of Tasks

Issue

I was wondering if there are any benefits to implementing the dispose feature for Tasks. Will this operate any different? Will this force the memory to clean up any faster?

Task updateTask = UpdateRemoteAsync()
await updateTask;

VS

using (Task updateTask = UpdateRemoteAsync())
{
    await updateTask;
}

Solution

In most cases no, when you dispose of a Task you are disposing the WaitHandle they allocate, and according to Microsoft, Since .NET Framework 4.5 the async/await functionality, WaitAll and WaitAny doesn’t allocate one at all unless you explicitly need to require IAsyncResult.AsyncWaitHandle.

Further reading: MS Dev Blog.

Answered By – HMZ

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.