Share via


AsyncLazy<T>.DisposeValueAsync Method

Definition

Disposes of the lazily-initialized value if disposable, and causes all subsequent attempts to obtain the value to fail.

public System.Threading.Tasks.Task DisposeValueAsync ();
member this.DisposeValueAsync : unit -> System.Threading.Tasks.Task
Public Function DisposeValueAsync () As Task

Returns

A task that completes when the value has been disposed of, or immediately if the value has already been disposed of or has been scheduled for disposal by a prior call.

Remarks

Calling this method will put this object into a disposed state where future calls to obtain the value will throw ObjectDisposedException.

If the value has already been produced and implements IDisposable, IAsyncDisposable, or IAsyncDisposable it will be disposed of. If the value factory has already started but has not yet completed, its value will be disposed of when the value factory completes.

If prior calls to obtain the value are in flight when this method is called, those calls may complete and their callers may obtain the value, although Dispose() may have been or will soon be called on the value, leading those users to experience a ObjectDisposedException.

Note all conditions based on the value implementing IDisposable or IAsyncDisposable is based on the actual value, rather than the T type argument. This means that although T may be IFoo (which does not implement IDisposable), the concrete type that implements IFoo may implement IDisposable and thus be treated as a disposable object as described above.

Applies to