Search code examples
c#.netdisposeusing

C# .Net Framework Disposing of Tasks


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.