Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle completed tasks in ApmAsyncFactory. #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/Nito.AsyncEx.Tasks/Interop/ApmAsyncFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Nito.AsyncEx.Synchronous;

Expand All @@ -18,6 +19,18 @@ public static class ApmAsyncFactory
/// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}

if (task.IsCompleted)
{
// we need this so it throws in case of faulted task
task.GetAwaiter().GetResult();
return new CompletedAsyncResult(state);
}

var tcs = new TaskCompletionSource<object?>(state, TaskCreationOptions.RunContinuationsAsynchronously);
SynchronizationContextSwitcher.NoContext(() => CompleteAsync(task, callback, tcs));
return tcs.Task;
Expand Down Expand Up @@ -54,7 +67,18 @@ private static async void CompleteAsync(Task task, AsyncCallback callback, TaskC
/// <returns>The result of the asynchronous operation, to be returned by the End method of the APM pattern.</returns>
public static void ToEnd(IAsyncResult asyncResult)
{
((Task)asyncResult).WaitAndUnwrapException();
if (asyncResult is Task task)
{
task.GetAwaiter().GetResult();
}
else if (asyncResult is CompletedAsyncResult)
{
// Do nothing
}
else
{
throw new ArgumentException("Invalid asyncResult", nameof(asyncResult));
}
}

/// <summary>
Expand All @@ -66,6 +90,16 @@ public static void ToEnd(IAsyncResult asyncResult)
/// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
public static IAsyncResult ToBegin<TResult>(Task<TResult> task, AsyncCallback callback, object state)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}

if (task.IsCompleted)
{
return new CompletedAsyncResult<TResult>(task.GetAwaiter().GetResult(), state);
}

var tcs = new TaskCompletionSource<TResult>(state, TaskCreationOptions.RunContinuationsAsynchronously);
SynchronizationContextSwitcher.NoContext(() => CompleteAsync(task, callback, tcs));
return tcs.Task;
Expand Down Expand Up @@ -101,7 +135,36 @@ private static async void CompleteAsync<TResult>(Task<TResult> task, AsyncCallba
/// <returns>The result of the asynchronous operation, to be returned by the End method of the APM pattern.</returns>
public static TResult ToEnd<TResult>(IAsyncResult asyncResult)
{
return ((Task<TResult>)asyncResult).WaitAndUnwrapException();
return asyncResult switch
{
Task<TResult> task => task.GetAwaiter().GetResult(),
CompletedAsyncResult<TResult> completedAsyncResult => completedAsyncResult.Result,
_ => throw new ArgumentException("Invalid asyncResult", nameof(asyncResult))
};
}

internal class CompletedAsyncResult<T> : CompletedAsyncResult
{
public CompletedAsyncResult(T result, object? state = null)
: base(state)
{
Result = result;
}

public T Result { get; }
}

internal class CompletedAsyncResult : IAsyncResult
{
public CompletedAsyncResult(object? state = null)
{
AsyncState = state;
}

public bool IsCompleted { get; } = true;
public bool CompletedSynchronously { get; } = true;
public WaitHandle? AsyncWaitHandle { get; }
public object? AsyncState { get; }
}
}
}