Skip to content

Commit

Permalink
Bringing samples up to speed
Browse files Browse the repository at this point in the history
  • Loading branch information
synhershko committed Jul 4, 2012
1 parent 5e8317a commit da814b5
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 166 deletions.
63 changes: 35 additions & 28 deletions Samples/WPFSampleApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,31 @@ private void CheckForUpdates()
{
UpdateManager updManager = UpdateManager.Instance;

updManager.CheckForUpdateAsync(hasUpdates =>
{
Action showUpdateAction = ShowUpdateWindow;

if (!hasUpdates)
{
// No updates were found, or an error has occured. We might want to check that...
if (updManager.LatestError == Errors.NoUpdatesFound)
{
MessageBox.Show("All is up to date!");
return;
}

MessageBox.Show(updManager.LatestError);
}

applyUpdates = true;

if (Dispatcher.CheckAccess())
showUpdateAction();
else
Dispatcher.Invoke(showUpdateAction);
});
}
updManager.BeginCheckForUpdates(asyncResult =>
{
Action showUpdateAction = ShowUpdateWindow;

if (asyncResult.IsCompleted)
{
// still need to check for caught exceptions if any and rethrow
((UpdateProcessAsyncResult) asyncResult).EndInvoke();

// No updates were found, or an error has occured. We might want to check that...
if (updManager.UpdatesAvailable == 0)
{
MessageBox.Show("All is up to date!");
return;
}
}

applyUpdates = true;

if (Dispatcher.CheckAccess())
showUpdateAction();
else
Dispatcher.Invoke(showUpdateAction);
}, null);
}

private void ShowUpdateWindow()
{
Expand All @@ -106,10 +107,16 @@ protected override void OnClosed(EventArgs e)
// Do any updates.
if (applyUpdates)
{
if (UpdateManager.Instance.PrepareUpdates())
UpdateManager.Instance.ApplyUpdates(false);
else
UpdateManager.Instance.CleanUp();
try
{
UpdateManager.Instance.PrepareUpdates();
}
catch
{
UpdateManager.Instance.CleanUp();
return;
}
UpdateManager.Instance.ApplyUpdates(false);
}
}

Expand Down
64 changes: 33 additions & 31 deletions Samples/WPFSampleApp/UpdateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using NAppUpdate.Framework;
using NAppUpdate.Framework.Common;

namespace NAppUpdate.SampleApp
{
Expand Down Expand Up @@ -40,37 +41,38 @@ private void InstallNow_Click(object sender, RoutedEventArgs e)
t.Start();
while (t.Enabled) { DoEvents(); }

_updateManager.PrepareUpdatesAsync(finished =>
{
if (finished)
{
// ApplyUpdates is a synchronous method by design. Make sure to save all user work before calling
// it as it might restart your application
// get out of the way so the console window isn't obstructed
Dispatcher d = Application.Current.Dispatcher;
d.BeginInvoke(new Action(Hide));
if (!_updateManager.ApplyUpdates(true))
{
d.BeginInvoke(new Action(this.Show)); // this.WindowState = WindowState.Normal;
MessageBox.Show("An error occurred while trying to install software updates");
}
else
{
d.BeginInvoke(new Action(Close));
}
_updateManager.CleanUp();
d.BeginInvoke(new Action(this.Close));
}
else
_updateManager.CleanUp();

Action close = Close;

if (Dispatcher.CheckAccess())
close();
else
Dispatcher.Invoke(close);
});
_updateManager.BeginPrepareUpdates(asyncResult =>
{
((UpdateProcessAsyncResult)asyncResult).EndInvoke();

// ApplyUpdates is a synchronous method by design. Make sure to save all user work before calling
// it as it might restart your application
// get out of the way so the console window isn't obstructed
Dispatcher d = Application.Current.Dispatcher;
d.BeginInvoke(new Action(Hide));
try
{
_updateManager.ApplyUpdates(true);
d.BeginInvoke(new Action(Close));
}
catch
{
d.BeginInvoke(new Action(this.Show));
// this.WindowState = WindowState.Normal;
MessageBox.Show(
"An error occurred while trying to install software updates");
}

_updateManager.CleanUp();
d.BeginInvoke(new Action(this.Close));

Action close = Close;

if (Dispatcher.CheckAccess())
close();
else
Dispatcher.Invoke(close);
}, null);
}

static void DoEvents()
Expand Down
10 changes: 2 additions & 8 deletions Samples/WinFormsProgressSample/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ private void btnStart_Click(object sender, EventArgs e)
btnStart.Enabled = false;
progressBar1.Step = 0;

UpdateManager.Instance.CheckForUpdateAsync(count => UpdateManager.Instance.PrepareUpdatesAsync(hasUpdates =>
UpdateManager.Instance.BeginCheckForUpdates(asyncResult => UpdateManager.Instance.BeginPrepareUpdates(ar2 =>
{
if (hasUpdates)
{
//UpdateManager.
// Instance.
// ApplyUpdates(false);
}
}
)
);
, null), null);
}


}
}
27 changes: 5 additions & 22 deletions Samples/WinFormsProgressSample/LengthyTask.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Conditions;
using NAppUpdate.Framework.Sources;
using NAppUpdate.Framework.Tasks;

namespace WinFormsProgressSample
{
public class LengthyTask : IUpdateTask
public class LengthyTask : UpdateTaskBase
{
public LengthyTask()
{
UpdateConditions = new BooleanCondition();
ExecutionStatus = TaskExecutionStatus.Pending;
}

public string Description{get; set; }

public BooleanCondition UpdateConditions { get; set; }

public TaskExecutionStatus ExecutionStatus { get; set; }

public event ReportProgressDelegate OnProgress;

public bool Prepare(IUpdateSource source)
public override bool Prepare(IUpdateSource source)
{
for (int i = 0; i < 50; i++)
{
Expand All @@ -47,12 +30,12 @@ public bool Prepare(IUpdateSource source)
return true;
}

public TaskExecutionStatus Execute(bool coldRun)
public override TaskExecutionStatus Execute(bool coldRun)
{
return coldRun ? TaskExecutionStatus.Successful : TaskExecutionStatus.RequiresAppRestart;
}

public bool Rollback()
public override bool Rollback()
{
return true;
}
Expand Down
Loading

0 comments on commit da814b5

Please sign in to comment.