我正在将大量使用异步编程模型(APM) 的 .Net 库移植到 Windows 商店应用程序库。
我想保持方法定义相同,以便使用该库的其他代码仅适用于 Windows 商店应用程序版本。(即我更喜欢它,没有异步等待模式)。
问题是尽管 Windows 商店应用程序似乎与 APM 兼容,但缺少具体的AsyncResult类。结果,无法获取BeginInvoke()
在回调和调用中调用的委托实例EndInvoke()
:
public MyObject EndDoSomething (IAsyncResult result)
{
// Retrieve the delegate.
AsyncResult asyncResult = (AsyncResult)result; // Not possible in Windows store app - no AsyncResult
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
// Call EndInvoke to retrieve the results.
MyObject rv = ((MyAsynchronousDelegate)asyncResult.AsyncDelegate).EndInvoke(result);
// Close the wait handel
result.AsyncWaitHandle.Close() // Close() is also missing
return rv;
}
那么一个人该做什么呢?