diff --git a/src/NetEvolve.Extensions.Hosting.WinForms/IWindowsFormsSynchronizationContextProvider.cs b/src/NetEvolve.Extensions.Hosting.WinForms/IWindowsFormsSynchronizationContextProvider.cs index d2c66f7..2470378 100644 --- a/src/NetEvolve.Extensions.Hosting.WinForms/IWindowsFormsSynchronizationContextProvider.cs +++ b/src/NetEvolve.Extensions.Hosting.WinForms/IWindowsFormsSynchronizationContextProvider.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Threading; using System.Threading.Tasks; /// @@ -42,18 +43,23 @@ public interface IWindowsFormsSynchronizationContextProvider /// Invokes the specified asynchronous action on the Windows Forms synchronization context. /// /// The action to be performed. + /// The cancellation token to be used. /// The result of the specified action. /// If is . - ValueTask InvokeAsync([NotNull] Action action); + ValueTask InvokeAsync([NotNull] Action action, CancellationToken cancellationToken = default); /// /// Invokes the specified asynchronous action on the Windows Forms synchronization context. /// /// The expected return type. /// The action to be performed. + /// The cancellation token to be used. /// The result of the specified action. /// If is . - ValueTask InvokeAsync([NotNull] Func action); + ValueTask InvokeAsync( + [NotNull] Func action, + CancellationToken cancellationToken = default + ); /// /// Invokes the specified asynchronous action on the Windows Forms synchronization context. @@ -62,10 +68,12 @@ public interface IWindowsFormsSynchronizationContextProvider /// The input type to be passed, which is to be processed. /// The action to be performed. /// The specified input. + /// The cancellation token to be used. /// The result of the specified action. /// If is . ValueTask InvokeAsync( [NotNull] Func action, - TInput input + TInput input, + CancellationToken cancellationToken = default ); } diff --git a/src/NetEvolve.Extensions.Hosting.WinForms/Internals/WindowsFormsSynchronizationContextProvider.cs b/src/NetEvolve.Extensions.Hosting.WinForms/Internals/WindowsFormsSynchronizationContextProvider.cs index a984099..3cdd7f0 100644 --- a/src/NetEvolve.Extensions.Hosting.WinForms/Internals/WindowsFormsSynchronizationContextProvider.cs +++ b/src/NetEvolve.Extensions.Hosting.WinForms/Internals/WindowsFormsSynchronizationContextProvider.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -62,7 +63,10 @@ public TResult Invoke([NotNull] Func action, T } /// - public async ValueTask InvokeAsync([NotNull] Action action) + public async ValueTask InvokeAsync( + [NotNull] Action action, + CancellationToken cancellationToken = default + ) { ArgumentNullException.ThrowIfNull(action); @@ -83,11 +87,14 @@ public async ValueTask InvokeAsync([NotNull] Action action) tcs ); - await tcs.Task.ConfigureAwait(true); + await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true); } /// - public async ValueTask InvokeAsync([NotNull] Func action) + public async ValueTask InvokeAsync( + [NotNull] Func action, + CancellationToken cancellationToken = default + ) { ArgumentNullException.ThrowIfNull(action); @@ -107,13 +114,14 @@ public async ValueTask InvokeAsync([NotNull] Func act }, tcs ); - return await tcs.Task.ConfigureAwait(true); + return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true); } /// public async ValueTask InvokeAsync( [NotNull] Func action, - TInput input + TInput input, + CancellationToken cancellationToken = default ) { ArgumentNullException.ThrowIfNull(action); @@ -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)