From dc807d8b8c791a9cf32eed45c54ddd0bb6485c22 Mon Sep 17 00:00:00 2001 From: Stephen DiJoseph Date: Thu, 14 Mar 2024 15:37:09 -0400 Subject: [PATCH 1/2] Add AsyncEventHandler for UpdateDetected event. --- .../MainWindow.xaml.cs | 3 ++- src/NetSparkle/SparkleUpdater.cs | 7 +++++-- src/NetSparkle/SparkleUpdaterEvents.cs | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/NetSparkle.Samples.HandleEventsYourself/MainWindow.xaml.cs b/src/NetSparkle.Samples.HandleEventsYourself/MainWindow.xaml.cs index d53a97b5..4e578d35 100644 --- a/src/NetSparkle.Samples.HandleEventsYourself/MainWindow.xaml.cs +++ b/src/NetSparkle.Samples.HandleEventsYourself/MainWindow.xaml.cs @@ -169,9 +169,10 @@ private async void UpdateAutomaticallyButton_Click(object sender, RoutedEventArg await _sparkle.CheckForUpdatesQuietly(); } - private void _sparkle_FullUpdate_UpdateDetected(object sender, Events.UpdateDetectedEventArgs e) + private Task _sparkle_FullUpdate_UpdateDetected(object sender, Events.UpdateDetectedEventArgs e) { RunFullUpdateUpdateStatusLabel.Text = "Found update..."; + return Task.CompletedTask; } private void _sparkle_FullUpdate_StartedDownloading(AppCastItem item, string path) diff --git a/src/NetSparkle/SparkleUpdater.cs b/src/NetSparkle/SparkleUpdater.cs index 22aa6e38..49a72a49 100644 --- a/src/NetSparkle/SparkleUpdater.cs +++ b/src/NetSparkle/SparkleUpdater.cs @@ -1824,7 +1824,7 @@ private async Task CheckForUpdates(bool isUserManuallyCheckingForUpd // handling everything. if (UpdateDetected != null) { - UpdateDetected(this, ev); // event's next action can change, here + await UpdateDetected(this, ev); // event's next action can change, here switch (ev.NextAction) { case NextUpdateAction.PerformUpdateUnattended: @@ -2048,7 +2048,10 @@ private async void OnWorkerDoWork(object sender, DoWorkEventArgs e) LatestVersion = updates[0], AppCastItems = updates }; - UpdateDetected?.Invoke(this, ev); + + if (UpdateDetected != null) + await UpdateDetected.Invoke(this, ev); + if (_cancelToken.IsCancellationRequested) { break; diff --git a/src/NetSparkle/SparkleUpdaterEvents.cs b/src/NetSparkle/SparkleUpdaterEvents.cs index b72a10b6..922a4ff4 100644 --- a/src/NetSparkle/SparkleUpdaterEvents.cs +++ b/src/NetSparkle/SparkleUpdaterEvents.cs @@ -1,18 +1,24 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Net; -using System.Text; using System.Diagnostics; +using System.Threading.Tasks; +using NetSparkleUpdater.Events; namespace NetSparkleUpdater { public partial class SparkleUpdater : IDisposable { + /// + /// Async event handler to allow awaiting of all event handlers. + /// + /// EventArgs to use with the handler. + public delegate Task AsyncEventHandler(object sender, TEventArgs e); + /// /// This event will be raised when an update check is about to be started /// public event LoopStartedOperation LoopStarted; + /// /// This event will be raised when an update check has finished /// @@ -22,11 +28,13 @@ public partial class SparkleUpdater : IDisposable /// Called when update check has just begun /// public event UpdateCheckStarted UpdateCheckStarted; + /// /// This event can be used to override the standard user interface /// process when an update is detected /// - public event UpdateDetected UpdateDetected; + public event AsyncEventHandler UpdateDetected; + /// /// Called when update check is all done. may have been /// called between the start and end of the update check. From 0030b154aab4fbd4228bfe38c4aa0169e349b5cd Mon Sep 17 00:00:00 2001 From: Stephen DiJoseph Date: Fri, 22 Mar 2024 15:31:57 -0400 Subject: [PATCH 2/2] Add Microsoft.VisualStudio.Threading package. - Use their implementation of AsyncEventHandler. --- src/NetSparkle/NetSparkle.csproj | 2 ++ src/NetSparkle/SparkleUpdater.cs | 5 +++-- src/NetSparkle/SparkleUpdaterEvents.cs | 7 +------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/NetSparkle/NetSparkle.csproj b/src/NetSparkle/NetSparkle.csproj index 7966853b..127a0ba4 100644 --- a/src/NetSparkle/NetSparkle.csproj +++ b/src/NetSparkle/NetSparkle.csproj @@ -19,6 +19,7 @@ true NetSparkleUpdater See https://github.com/NetSparkleUpdater/NetSparkle/releases for all release information and to file issues/pull requests for this project. + VSTHRD200, VSTHRD100, VSTHRD002, VSTHRD001 true @@ -100,6 +101,7 @@ + diff --git a/src/NetSparkle/SparkleUpdater.cs b/src/NetSparkle/SparkleUpdater.cs index 49a72a49..b229af1f 100644 --- a/src/NetSparkle/SparkleUpdater.cs +++ b/src/NetSparkle/SparkleUpdater.cs @@ -19,6 +19,7 @@ using NetSparkleUpdater.AssemblyAccessors; using System.Text; using System.Globalization; +using Microsoft.VisualStudio.Threading; #if (NETSTANDARD || NET6 || NET7 || NET8) using System.Runtime.InteropServices; #endif @@ -1824,7 +1825,7 @@ private async Task CheckForUpdates(bool isUserManuallyCheckingForUpd // handling everything. if (UpdateDetected != null) { - await UpdateDetected(this, ev); // event's next action can change, here + await UpdateDetected.InvokeAsync(this, ev); // event's next action can change, here switch (ev.NextAction) { case NextUpdateAction.PerformUpdateUnattended: @@ -2050,7 +2051,7 @@ private async void OnWorkerDoWork(object sender, DoWorkEventArgs e) }; if (UpdateDetected != null) - await UpdateDetected.Invoke(this, ev); + await UpdateDetected.InvokeAsync(this, ev); if (_cancelToken.IsCancellationRequested) { diff --git a/src/NetSparkle/SparkleUpdaterEvents.cs b/src/NetSparkle/SparkleUpdaterEvents.cs index 922a4ff4..ff207d53 100644 --- a/src/NetSparkle/SparkleUpdaterEvents.cs +++ b/src/NetSparkle/SparkleUpdaterEvents.cs @@ -2,18 +2,13 @@ using System.ComponentModel; using System.Diagnostics; using System.Threading.Tasks; +using Microsoft.VisualStudio.Threading; using NetSparkleUpdater.Events; namespace NetSparkleUpdater { public partial class SparkleUpdater : IDisposable { - /// - /// Async event handler to allow awaiting of all event handlers. - /// - /// EventArgs to use with the handler. - public delegate Task AsyncEventHandler(object sender, TEventArgs e); - /// /// This event will be raised when an update check is about to be started ///