我有以下几种方法:
public int getData() { return 2; } // suppose it is slow and takes 20 sec
// pseudocode
public int GetPreviousData()
{
Task<int> t = new Task<int>(() => getData());
return _cachedData; // some previous value
_cachedData = t.Result; // _cachedData == 2
}
我不想等待已经运行的操作结果。
我想返回_cachedData
并在Task
完成后更新它。
如何实现?我正在使用.net framework 4.5.2