Skip to content

Commit

Permalink
Merge pull request #73 from synhershko/72-update-manager-args
Browse files Browse the repository at this point in the history
#72 Cleaned up inconsistent method signatures
  • Loading branch information
robinwassen committed Feb 18, 2016
2 parents 14fd0a7 + 440278e commit 46a7e67
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 43 deletions.
3 changes: 2 additions & 1 deletion Samples/WinFormsSampleApp/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
70 changes: 28 additions & 42 deletions src/NAppUpdate.Framework/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,37 +116,34 @@ private void TaskProgressCallback(UpdateProgressInfo currentStatus, IUpdateTask
#region Step 1 - Check for updates

/// <summary>
/// Check for update synchronously, using the default update source
/// Check for updates synchronously
/// </summary>
public void CheckForUpdates()
{
CheckForUpdates(UpdateSource);
}

/// <summary>
/// Check for updates synchronouly
/// </summary>
/// <param name="source">Updates source to use</param>
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)
Expand All @@ -164,44 +161,33 @@ public void CheckForUpdates(IUpdateSource source)
/// <summary>
/// Check for updates asynchronously
/// </summary>
/// <param name="source">Update source to use</param>
/// <param name="callback">Callback function to call when done; can be null</param>
/// <param name="state">Allows the caller to preserve state; can be null</param>
public IAsyncResult BeginCheckForUpdates(IUpdateSource source, AsyncCallback callback, Object state)
public IAsyncResult BeginCheckForUpdates(AsyncCallback callback, Object state)
{
// Create IAsyncResult object identifying the
// asynchronous operation
var ar = new UpdateProcessAsyncResult(callback, state);

// 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
}

/// <summary>
/// Check for updates asynchronously
/// </summary>
/// <param name="callback">Callback function to call when done; can be null</param>
/// <param name="state">Allows the caller to preserve state; can be null</param>
public IAsyncResult BeginCheckForUpdates(AsyncCallback callback, Object state)
{
return BeginCheckForUpdates(UpdateSource, callback, state);
}

/// <summary>
/// Block until previously-called CheckForUpdates complete
/// </summary>
Expand Down

0 comments on commit 46a7e67

Please sign in to comment.