Task.Dispose Method

Definition

Releases all resources used by the current instance of the Task class.

Overloads

Dispose(Boolean)

Disposes the Task, releasing all of its unmanaged resources.

Dispose()

Releases all resources used by the current instance of the Task class.

Dispose(Boolean)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Disposes the Task, releasing all of its unmanaged resources.

C#
protected virtual void Dispose(bool disposing);

Parameters

disposing
Boolean

A Boolean value that indicates whether this method is being called due to a call to Dispose().

Exceptions

The task is not in one of the final states: RanToCompletion, Faulted, or Canceled.

Remarks

The Task class implements the IDisposable interface because internally it uses resources that also implement IDisposable. However, particularly if your app targets .NET Framework 4.5 or later, there is no need to call Dispose unless performance or scalability testing indicates that, based on your usage patterns, your app's performance would be improved by disposing of tasks. For more information, see Do I need to dispose of Tasks? in the Parallel Programming with .NET blog.

Thread Safety

Unlike most of the members of the Task class, this method is not thread-safe.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Dispose()

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Releases all resources used by the current instance of the Task class.

C#
public void Dispose();

Implements

Exceptions

The task is not in one of the final states: RanToCompletion, Faulted, or Canceled.

Remarks

The Task class implements the IDisposable interface because internally it uses resources that also implement IDisposable. However, particularly if your app targets .NET Framework 4.5 or later, there is no need to call Dispose unless performance or scalability testing indicates that, based on your usage patterns, your app's performance would be improved by disposing of tasks. For more information, see Do I need to dispose of Tasks? in the Parallel Programming with .NET blog.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1