Skip to content

Commit

Permalink
chore: Added CancellationToken parameter to async Methods of `IWind…
Browse files Browse the repository at this point in the history
…owsFormsSynchronizationContextProvider`
  • Loading branch information
samtrion committed Mar 25, 2024
1 parent 7f3b8f4 commit d53ed7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
Expand Down Expand Up @@ -42,18 +43,23 @@ public interface IWindowsFormsSynchronizationContextProvider
/// Invokes the specified asynchronous action on the Windows Forms synchronization context.
/// </summary>
/// <param name="action">The action to be performed.</param>
/// <param name="cancellationToken">The cancellation token to be used.</param>
/// <returns>The result of the specified action.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="action"/> is <see langword="null"/>.</exception>
ValueTask InvokeAsync([NotNull] Action action);
ValueTask InvokeAsync([NotNull] Action action, CancellationToken cancellationToken = default);

/// <summary>
/// Invokes the specified asynchronous action on the Windows Forms synchronization context.
/// </summary>
/// <typeparam name="TResult">The expected return type.</typeparam>
/// <param name="action">The action to be performed.</param>
/// <param name="cancellationToken">The cancellation token to be used.</param>
/// <returns>The result of the specified action.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="action"/> is <see langword="null"/>.</exception>
ValueTask<TResult> InvokeAsync<TResult>([NotNull] Func<TResult> action);
ValueTask<TResult> InvokeAsync<TResult>(
[NotNull] Func<TResult> action,
CancellationToken cancellationToken = default
);

/// <summary>
/// Invokes the specified asynchronous action on the Windows Forms synchronization context.
Expand All @@ -62,10 +68,12 @@ public interface IWindowsFormsSynchronizationContextProvider
/// <typeparam name="TInput">The input type to be passed, which is to be processed.</typeparam>
/// <param name="action">The action to be performed.</param>
/// <param name="input">The specified input.</param>
/// <param name="cancellationToken">The cancellation token to be used.</param>
/// <returns>The result of the specified action.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="action"/> is <see langword="null"/>.</exception>
ValueTask<TResult> InvokeAsync<TResult, TInput>(
[NotNull] Func<TInput, TResult> action,
TInput input
TInput input,
CancellationToken cancellationToken = default
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand Down Expand Up @@ -62,7 +63,10 @@ public TResult Invoke<TResult, TInput>([NotNull] Func<TInput, TResult> action, T
}

/// <inheritdoc/>
public async ValueTask InvokeAsync([NotNull] Action action)
public async ValueTask InvokeAsync(
[NotNull] Action action,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(action);

Expand All @@ -83,11 +87,14 @@ public async ValueTask InvokeAsync([NotNull] Action action)
tcs
);

await tcs.Task.ConfigureAwait(true);
await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true);
}

/// <inheritdoc/>
public async ValueTask<TResult> InvokeAsync<TResult>([NotNull] Func<TResult> action)
public async ValueTask<TResult> InvokeAsync<TResult>(
[NotNull] Func<TResult> action,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(action);

Expand All @@ -107,13 +114,14 @@ public async ValueTask<TResult> InvokeAsync<TResult>([NotNull] Func<TResult> act
},
tcs
);
return await tcs.Task.ConfigureAwait(true);
return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true);
}

/// <inheritdoc/>
public async ValueTask<TResult> InvokeAsync<TResult, TInput>(
[NotNull] Func<TInput, TResult> action,
TInput input
TInput input,
CancellationToken cancellationToken = default
)
{
ArgumentNullException.ThrowIfNull(action);
Expand All @@ -134,7 +142,7 @@ TInput input
},
tcs
);
return await tcs.Task.ConfigureAwait(true);
return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true);
}

private void Dispose(bool disposing)
Expand Down

0 comments on commit d53ed7e

Please sign in to comment.