-
-
Notifications
You must be signed in to change notification settings - Fork 357
AsyncLazy
StephenCleary edited this page Sep 4, 2014
·
1 revision
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 await
ed 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 await
s on that instance complete immediately.
// 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();
}
The full API is supported on all platforms.