I am generally wary of implementing interfaces partially. However, IAsyncResult
is a bit of a special case, given that it supports several quite different usage patterns. How often do you use/see used the AsyncState
/AsyncCallback
pattern, as opposed to just calling EndInvoke
, using AsyncWaitHandle
, or polling IsCompleted
(yuck)?
Related question: Detecting that a ThreadPool WorkItem has completed/waiting for completion.
Consider this class (very approximate, locking needed):
public class Concurrent<T> {
private ManualResetEvent _resetEvent;
private T _result;
public Concurrent(Func<T> f) {
ThreadPool.QueueUserWorkItem(_ => {
_result = f();
IsCompleted = true;
if (_resetEvent != null)
_resetEvent.Set();
});
}
public WaitHandle WaitHandle {
get {
if (_resetEvent == null)
_resetEvent = new ManualResetEvent(IsCompleted);
return _resetEvent;
}
public bool IsCompleted {get; private set;}
...
It has WaitHandle
(lazily created, just as described in IAsyncResult
documentation) and IsCompleted
, but I don't see a sensible implementation for AsyncState
({return null;}
?). So does it make sense for it to implement IAsyncResult
? Note that Task
in the Parallel Extensions library does implement IAsyncResult
, but only IsCompleted
is implemented implicitly.