Skip to content
StephenCleary edited this page Sep 4, 2014 · 1 revision

Overview

The AsyncLazy<T> type enables asynchronous lazy initialization, similar to Stephen Toub's AsyncLazy.

An AsyncLazy<T> instance is constructed with a factory method (which may be synchronous or asynchronous). When the AsyncLazy<T> instance is awaited or its Start method is called, the factory method starts on a thread pool thread. The factory method is only executed once. Once the factory method has completed, all future awaits on that instance complete immediately.

API

// Provides support for asynchronous lazy initialization. This type is fully threadsafe.
public sealed class AsyncLazy<T>
{
  // Initializes a new instance of the AsyncLazy<T> class.
  public AsyncLazy(Func<T> factory);

  // Initializes a new instance of the AsyncLazy<T> class.
  public AsyncLazy(Func<Task<T>> factory);

  // Gets a semi-unique identifier for this asynchronous lazy instance.
  public int Id { get; }

  // Asynchronous infrastructure support.
  // This method permits instances of AsyncLazy<T> to be await'ed.
  public <unspecified> GetAwaiter();

  // Starts the asynchronous initialization, if it has not already started.
  public void Start();
}

Platform Support

The full API is supported on all platforms.