Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Created September 9, 2017 09:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save idiotandrobot/de86b6195a56f02d516b8cd81329d60c to your computer and use it in GitHub Desktop.
AsyncLazy<T> v2
// via http://blog.stephencleary.com/2012/08/asynchronous-lazy-initialization.html
/// <summary>
/// Provides support for asynchronous lazy initialization. This type is fully threadsafe.
/// </summary>
/// <typeparam name="T">The type of object that is being asynchronously initialized.</typeparam>
public sealed class AsyncLazy<T>
{
/// <summary>
/// The underlying lazy task.
/// </summary>
private readonly Lazy<Task<T>> instance;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncLazy&lt;T&gt;"/> class.
/// </summary>
/// <param name="factory">The delegate that is invoked on a background thread to produce the value when it is needed.</param>
public AsyncLazy(Func<T> factory)
{
instance = new Lazy<Task<T>>(() => Task.Run(factory));
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncLazy&lt;T&gt;"/> class.
/// </summary>
/// <param name="factory">The asynchronous delegate that is invoked on a background thread to produce the value when it is needed.</param>
public AsyncLazy(Func<Task<T>> factory)
{
instance = new Lazy<Task<T>>(() => Task.Run(factory));
}
/// <summary>
/// Asynchronous infrastructure support. This method permits instances of <see cref="AsyncLazy&lt;T&gt;"/> to be await'ed.
/// </summary>
public TaskAwaiter<T> GetAwaiter()
{
return instance.Value.GetAwaiter();
}
/// <summary>
/// Starts the asynchronous initialization, if it has not already started.
/// </summary>
public void Start()
{
var unused = instance.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment