Last active
January 4, 2016 02:29
-
-
Save dck-jp/8555762 to your computer and use it in GitHub Desktop.
PrefetchedProperty ver.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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