diff --git a/Samples/WinFormsSampleApp/frmMain.cs b/Samples/WinFormsSampleApp/frmMain.cs index a4e691ac..71012f90 100644 --- a/Samples/WinFormsSampleApp/frmMain.cs +++ b/Samples/WinFormsSampleApp/frmMain.cs @@ -81,6 +81,7 @@ private void CheckForUpdates(IUpdateSource source) { // Get a local pointer to the UpdateManager instance UpdateManager updManager = UpdateManager.Instance; + updManager.UpdateSource = source; // Only check for updates if we haven't done so already if (updManager.State != UpdateManager.UpdateProcessState.NotChecked) @@ -94,7 +95,7 @@ private void CheckForUpdates(IUpdateSource source) // Check for updates - returns true if relevant updates are found (after processing all the tasks and // conditions) // Throws exceptions in case of bad arguments or unexpected results - updManager.CheckForUpdates(source); + updManager.CheckForUpdates(); } catch (Exception ex) { diff --git a/src/NAppUpdate.Framework/UpdateManager.cs b/src/NAppUpdate.Framework/UpdateManager.cs index 91a7e39e..36ffbd19 100644 --- a/src/NAppUpdate.Framework/UpdateManager.cs +++ b/src/NAppUpdate.Framework/UpdateManager.cs @@ -116,37 +116,34 @@ private void TaskProgressCallback(UpdateProgressInfo currentStatus, IUpdateTask #region Step 1 - Check for updates /// - /// Check for update synchronously, using the default update source + /// Check for updates synchronously /// public void CheckForUpdates() - { - CheckForUpdates(UpdateSource); - } - - /// - /// Check for updates synchronouly - /// - /// Updates source to use - public void CheckForUpdates(IUpdateSource source) { if (IsWorking) + { throw new InvalidOperationException("Another update process is already in progress"); + } + else if (UpdateFeedReader == null) + { + throw new InvalidOperationException("UpdateFeedReader must be set before calling CheckForUpdates()"); + } + else if (UpdateSource == null) + { + throw new InvalidOperationException("UpdateSource must be set before calling CheckForUpdates()"); + } using (WorkScope.New(isWorking => IsWorking = isWorking)) { - if (UpdateFeedReader == null) - throw new ArgumentException("An update feed reader is required; please set one before checking for updates"); - - if (source == null) - throw new ArgumentException("An update source was not specified"); - if (State != UpdateProcessState.NotChecked) + { throw new InvalidOperationException("Already checked for updates; to reset the current state call CleanUp()"); + } lock (UpdatesToApply) { UpdatesToApply.Clear(); - var tasks = UpdateFeedReader.Read(source.GetUpdatesFeed()); + var tasks = UpdateFeedReader.Read(UpdateSource.GetUpdatesFeed()); foreach (var t in tasks) { if (ShouldStop) @@ -164,10 +161,9 @@ public void CheckForUpdates(IUpdateSource source) /// /// Check for updates asynchronously /// - /// Update source to use /// Callback function to call when done; can be null /// Allows the caller to preserve state; can be null - public IAsyncResult BeginCheckForUpdates(IUpdateSource source, AsyncCallback callback, Object state) + public IAsyncResult BeginCheckForUpdates(AsyncCallback callback, Object state) { // Create IAsyncResult object identifying the // asynchronous operation @@ -175,33 +171,23 @@ public IAsyncResult BeginCheckForUpdates(IUpdateSource source, AsyncCallback cal // Use a thread pool thread to perform the operation ThreadPool.QueueUserWorkItem(o => - { - try - { - // Perform the operation; if sucessful set the result - CheckForUpdates(source ?? UpdateSource); - ar.SetAsCompleted(null, false); - } - catch (Exception e) - { - // If operation fails, set the exception - ar.SetAsCompleted(e, false); - } - }, ar); + { + try + { + // Perform the operation; if sucessful set the result + CheckForUpdates(); + ar.SetAsCompleted(null, false); + } + catch (Exception e) + { + // If operation fails, set the exception + ar.SetAsCompleted(e, false); + } + }, ar); return ar; // Return the IAsyncResult to the caller } - /// - /// Check for updates asynchronously - /// - /// Callback function to call when done; can be null - /// Allows the caller to preserve state; can be null - public IAsyncResult BeginCheckForUpdates(AsyncCallback callback, Object state) - { - return BeginCheckForUpdates(UpdateSource, callback, state); - } - /// /// Block until previously-called CheckForUpdates complete ///