Skip to content

Commit

Permalink
* Removed ApplicationExitEvent.
Browse files Browse the repository at this point in the history
* Fixed issue where calling Start from remind later timer causes crash.
* Now developers can use timer to check for updates frequently. This closes #13.
  • Loading branch information
ravibpatel committed Jun 19, 2017
1 parent 89e0579 commit 2f9616c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 75 deletions.
137 changes: 84 additions & 53 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net;
using System.Net.Cache;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using AutoUpdaterDotNET.Properties;
Expand Down Expand Up @@ -39,6 +40,8 @@ public enum RemindLaterFormat
/// </summary>
public static class AutoUpdater
{
private static System.Timers.Timer _remindLaterTimer;

internal static String ChangeLogURL;

internal static String DownloadURL;
Expand All @@ -53,6 +56,8 @@ public static class AutoUpdater

internal static bool IsWinFormsApplication;

internal static bool Running;

/// <summary>
/// Set the Application Title shown in Update dialog. Although AutoUpdater.NET will get it automatically, you can set this property if you like to give custom Title.
/// </summary>
Expand All @@ -67,6 +72,13 @@ public static class AutoUpdater
/// Opens the download url in default browser if true. Very usefull if you have portable application.
/// </summary>
public static bool OpenDownloadPage;

/// <summary>
/// Sets the current culture of the auto update notification window. Set this value if your application supports
/// functionalty to change the languge of the application.
/// </summary>
public static CultureInfo CurrentCulture;


/// <summary>
/// If this is true users can see the skip button.
Expand Down Expand Up @@ -113,39 +125,40 @@ public static class AutoUpdater
/// <summary>
/// Start checking for new version of application and display dialog to the user if update is available.
/// </summary>
public static void Start()
/// <param name="myAssembly">Assembly to use for version checking.</param>
public static void Start(Assembly myAssembly = null)
{
Start(AppCastURL);
Start(AppCastURL, myAssembly);
}

/// <summary>
/// A delegate type to handle how to exit the application after update is downloaded.
/// </summary>
public delegate void ApplicationExitEventHandler();

/// <summary>
/// An event that developers can use to exit the application gracefully.
/// </summary>
public static event ApplicationExitEventHandler ApplicationExitEvent;

/// <summary>
/// Start checking for new version of application and display dialog to the user if update is available.
/// </summary>
/// <param name="appCast">URL of the xml file that contains information about latest version of the application.</param>
/// <param name="myAssembly">Assembly to use for version checking.</param>
public static void Start(String appCast, Assembly myAssembly = null)
{
AppCastURL = appCast;
if (!Running && _remindLaterTimer == null)
{
Running = true;

if (CurrentCulture == null)
{
CurrentCulture = CultureInfo.CurrentCulture;
}

IsWinFormsApplication = Application.MessageLoop;
AppCastURL = appCast;

var backgroundWorker = new BackgroundWorker();
IsWinFormsApplication = Application.MessageLoop;

backgroundWorker.DoWork += BackgroundWorkerDoWork;
var backgroundWorker = new BackgroundWorker();

backgroundWorker.RunWorkerCompleted += BackgroundWorkerOnRunWorkerCompleted;
backgroundWorker.DoWork += BackgroundWorkerDoWork;

backgroundWorker.RunWorkerAsync(myAssembly ?? Assembly.GetEntryAssembly());
backgroundWorker.RunWorkerCompleted += BackgroundWorkerOnRunWorkerCompleted;

backgroundWorker.RunWorkerAsync(myAssembly ?? Assembly.GetEntryAssembly());
}
}

private static void BackgroundWorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs)
Expand Down Expand Up @@ -174,8 +187,33 @@ private static void BackgroundWorkerOnRunWorkerCompleted(object sender, RunWorke
{
Application.EnableVisualStyles();
}
var updateForm = new UpdateForm();
updateForm.Show();
var backgroundworker = new BackgroundWorker();
backgroundworker.DoWork += delegate (object o, DoWorkEventArgs eventArgs)
{
eventArgs.Cancel = true;
Thread thread = new Thread(new ThreadStart(delegate
{
var updateForm = new UpdateForm();
if (updateForm.ShowDialog().Equals(DialogResult.OK))
{
eventArgs.Cancel = false;
}
}));
thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
};
backgroundworker.RunWorkerCompleted +=
delegate (object o, RunWorkerCompletedEventArgs eventArgs)
{
if (!eventArgs.Cancelled)
{
Exit();
}
};
backgroundworker.RunWorkerAsync();
return;
}
else
{
Expand All @@ -196,6 +234,7 @@ private static void BackgroundWorkerOnRunWorkerCompleted(object sender, RunWorke
}
}
}
Running = false;
}
}
}
Expand Down Expand Up @@ -385,39 +424,30 @@ private static string GetURL(Uri baseUri, XmlNode xmlNode)
return temp;
}

internal static void Exit(Form ownerForm)
private static void Exit()
{
if (ApplicationExitEvent != null)
{
ApplicationExitEvent();
}
else
var currentProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
{
var currentProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
if (process.Id != currentProcess.Id)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
}
process.Kill();
}
}

ownerForm.Close();

if (IsWinFormsApplication)
{
Application.Exit();
}
#if NETWPF
else if (System.Windows.Application.Current != null)
{
System.Windows.Application.Current.Shutdown();
}
#endif
else
{
Environment.Exit(0);
}
if (IsWinFormsApplication)
{
Application.Exit();
}
#if NETWPF
else if (System.Windows.Application.Current != null)
{
System.Windows.Application.Current.Shutdown();
}
#endif
else
{
Environment.Exit(0);
}
}

Expand All @@ -434,16 +464,17 @@ private static Attribute GetAttribute(Assembly assembly, Type attributeType)
internal static void SetTimer(DateTime remindLater)
{
TimeSpan timeSpan = remindLater - DateTime.Now;
var timer = new System.Timers.Timer
_remindLaterTimer = new System.Timers.Timer
{
Interval = (int) timeSpan.TotalMilliseconds
Interval = (int) timeSpan.TotalMilliseconds,
};
timer.Elapsed += delegate
_remindLaterTimer.Elapsed += delegate
{
timer.Stop();
_remindLaterTimer.Stop();
_remindLaterTimer = null;
Start();
};
timer.Start();
_remindLaterTimer.Start();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion AutoUpdater.NET/DownloadUpdateDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void OnDownloadComplete(object sender, AsyncCompletedEventArgs e)
throw;
}

AutoUpdater.Exit(this);
Close();
}

private static string GetFileName(string url, string httpWebRequestMethod = "HEAD")
Expand Down
Binary file modified AutoUpdater.NET/Resources/ZipExtractor.exe
Binary file not shown.
4 changes: 1 addition & 3 deletions AutoUpdater.NET/UpdateForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions AutoUpdater.NET/UpdateForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ private void ButtonUpdateClick(object sender, EventArgs e)
var processStartInfo = new ProcessStartInfo(AutoUpdater.DownloadURL);

Process.Start(processStartInfo);

DialogResult = DialogResult.OK;
}
else
{
if (AutoUpdater.DownloadUpdate())
{
Close();
DialogResult = DialogResult.OK;
}
}
}
Expand All @@ -79,7 +81,6 @@ private void ButtonSkipClick(object sender, EventArgs e)
{
updateKey.SetValue("version", AutoUpdater.CurrentVersion.ToString());
updateKey.SetValue("skip", 1);
Close();
}
}
}
Expand All @@ -97,14 +98,18 @@ private void ButtonRemindLaterClick(object sender, EventArgs e)
AutoUpdater.RemindLaterTimeSpan = remindLaterForm.RemindLaterFormat;
AutoUpdater.RemindLaterAt = remindLaterForm.RemindLaterAt;
}
if (dialogResult.Equals(DialogResult.Abort))
else if (dialogResult.Equals(DialogResult.Abort))
{
if (AutoUpdater.DownloadUpdate())
{
Close();
DialogResult = DialogResult.OK;
}
return;
}
else
{
return;
}
}

using (RegistryKey updateKey = Registry.CurrentUser.CreateSubKey(AutoUpdater.RegistryLocation))
Expand Down Expand Up @@ -132,7 +137,12 @@ private void ButtonRemindLaterClick(object sender, EventArgs e)
AutoUpdater.SetTimer(remindLaterDateTime);
}
}
Close();
DialogResult = DialogResult.Cancel;
}

private void UpdateForm_FormClosed(object sender, FormClosedEventArgs e)
{
AutoUpdater.Running = false;
}
}
}
2 changes: 1 addition & 1 deletion AutoUpdater.NET/UpdateForm.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<value>Mettre à jour</value>
</data>
<data name="buttonRemindLater.Text" xml:space="preserve">
<value>Rappelle plus tard</value>
<value>Rappeler plus tard</value>
</data>
<data name="buttonSkip.Text" xml:space="preserve">
<value>Passer cette version</value>
Expand Down
16 changes: 15 additions & 1 deletion AutoUpdaterTestWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Globalization;
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Threading;
using AutoUpdaterDotNET;

namespace AutoUpdaterTestWPF
Expand All @@ -17,7 +20,18 @@ public MainWindow()
Assembly assembly = Assembly.GetEntryAssembly();
LabelVersion.Content = $"Current Version : {assembly.GetName().Version}";
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
AutoUpdater.LetUserSelectRemindLater = true;
AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Minutes;
AutoUpdater.RemindLaterAt = 1;
AutoUpdater.ReportErrors = true;
System.Timers.Timer timer = new System.Timers.Timer {Interval = 2 * 60 * 1000};
timer.Elapsed += delegate(object sender, ElapsedEventArgs args)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTestWPF.xml");
}));
};
timer.Start();
}

private void ButtonCheckForUpdate_Click(object sender, RoutedEventArgs e)
Expand Down
Loading

0 comments on commit 2f9616c

Please sign in to comment.