Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Last active January 4, 2016 02:29
Show Gist options
  • Select an option

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

Select an option

Save dck-jp/8555762 to your computer and use it in GitHub Desktop.
PrefetchedProperty ver.2
public class Class1
{
private Task _LoadPropertyATask;
private string _PropertyA;
public string PropertyA
{
get
{
if (_LoadPropertyATask == null) Initialize(); //同期読み出しの場合、非同期読み出し命令をここで実行
//(即、Wait()がかかるので実質的に同期読み出し)
if (_PropertyA != null) return _PropertyA; //非同期読み出し途中の場合
_LoadPropertyATask.Wait(); //※ 非同期読み出しが完了している場合はスルーされる
return _PropertyA;
}
}
public void Initialize()
{
_LoadPropertyATask = LoadPropertyAAsync();
}
private void LoadPropertyA()
{
Thread.Sleep(1000);
_PropertyA = "abc";
}
private async Task LoadPropertyAAsync()
{
await new TaskFactory().StartNew(() => LoadPropertyA());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment