Created
January 22, 2014 03:42
-
-
Save dck-jp/8553153 to your computer and use it in GitHub Desktop.
Getter that prefetched the value asynchronously
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
| 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