private static Task <T> FromAsync <T>(Func <AsyncCallback, IAsyncResult> begin, Func <IAsyncResult, T> end)
        {
            var tcs = new DispatchingTaskCompletionSource <T>();

            try
            {
                var result = begin(ar =>
                {
                    if (!ar.CompletedSynchronously)
                    {
                        CompleteAsync(tcs, ar, end);
                    }
                });

                if (result.CompletedSynchronously)
                {
                    CompleteAsync(tcs, result, end);
                }
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }

            return(tcs.Task);
        }
 private static void CompleteAsync <T>(DispatchingTaskCompletionSource <T> tcs, IAsyncResult ar, Func <IAsyncResult, T> end)
 {
     try
     {
         tcs.TrySetResult(end(ar));
     }
     catch (OperationCanceledException)
     {
         tcs.TrySetCanceled();
     }
     catch (Exception ex)
     {
         tcs.TrySetException(ex);
     }
 }
示例#3
0
 public AckInfo()
 {
     Tcs     = new DispatchingTaskCompletionSource <object>();
     Created = DateTime.UtcNow;
 }