Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Created January 22, 2014 03:42
Show Gist options
  • Select an option

  • Save dck-jp/8553153 to your computer and use it in GitHub Desktop.

Select an option

Save dck-jp/8553153 to your computer and use it in GitHub Desktop.
Getter that prefetched the value asynchronously
class Class1
{
private Task<string> _propertyALoadTask;
private string _PropertyA;
public string PropertyA{
get
{
if(_PropertyA != null) return _PropertyA;
_propertyALoadTask.Wait();
_PropertyA = _propertyALoadTask.Result;
return _PropertyA;
}
}
public Class1()
{
_propertyALoadTask = new Task<string>(
() => {Thread.Sleep(1000); return "abc";});
LoadPropertyA();
}
private void LoadPropertyA()
{
_propertyALoadTask.Start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment