Skip to content

Commit

Permalink
Fixed #11 causing AutoUpdater.NET to not kill WPF application after u…
Browse files Browse the repository at this point in the history
…pdate finishes downloading.
  • Loading branch information
ravibpatel committed Jun 15, 2017
1 parent 1ea1a21 commit 14b8f68
Show file tree
Hide file tree
Showing 25 changed files with 668 additions and 139 deletions.
34 changes: 7 additions & 27 deletions AutoUpdater.NET.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
VisualStudioVersion = 15.0.26403.7
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoUpdater.NET", "AutoUpdater.NET\AutoUpdater.NET.csproj", "{FB9E7E6B-B19F-4F37-A708-2996190CEF13}"
ProjectSection(ProjectDependencies) = postProject
Expand All @@ -12,50 +12,30 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoUpdaterTest", "AutoUpda
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZipExtractor", "ZipExtractor\ZipExtractor.csproj", "{91DE558C-6DB8-429B-A069-C0491DCFF15B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoUpdaterTestWPF", "AutoUpdaterTestWPF\AutoUpdaterTestWPF.csproj", "{9AEE4F21-5ED5-4C7E-9249-8A680123B5E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Debug|x86.ActiveCfg = Debug|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Release|Any CPU.Build.0 = Release|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{FB9E7E6B-B19F-4F37-A708-2996190CEF13}.Release|x86.ActiveCfg = Release|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|x86.ActiveCfg = Debug|x86
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Debug|x86.Build.0 = Debug|x86
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|Any CPU.Build.0 = Release|Any CPU
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|Mixed Platforms.ActiveCfg = Release|x86
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|Mixed Platforms.Build.0 = Release|x86
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|x86.ActiveCfg = Release|x86
{FD5AE762-C630-49F8-B814-FCF70E7838D1}.Release|x86.Build.0 = Release|x86
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|x86.ActiveCfg = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Debug|x86.Build.0 = Debug|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|Any CPU.Build.0 = Release|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|x86.ActiveCfg = Release|Any CPU
{91DE558C-6DB8-429B-A069-C0491DCFF15B}.Release|x86.Build.0 = Release|Any CPU
{9AEE4F21-5ED5-4C7E-9249-8A680123B5E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AEE4F21-5ED5-4C7E-9249-8A680123B5E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AEE4F21-5ED5-4C7E-9249-8A680123B5E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AEE4F21-5ED5-4C7E-9249-8A680123B5E3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
27 changes: 22 additions & 5 deletions AutoUpdater.NET/AutoUpdater.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AutoUpdaterDotNET</RootNamespace>
<AssemblyName>AutoUpdater.NET</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\Debug\$(TargetFrameworkVersion)</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\AutoUpdater.NET.XML</DocumentationFile>
<DocumentationFile>bin\Debug\$(TargetFrameworkVersion)\AutoUpdater.NET.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>bin\Release\$(TargetFrameworkVersion)</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\AutoUpdater.NET.XML</DocumentationFile>
<DocumentationFile>bin\Release\$(TargetFrameworkVersion)\AutoUpdater.NET.XML</DocumentationFile>
<UseVSHostingProcess>false</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -39,6 +40,22 @@
<PropertyGroup>
<AssemblyOriginatorKeyFile>AutoUpdater.NET.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<!-- WPF Specific -->
<PropertyGroup Condition=" '$(TargetFrameworkVersion)' != 'v2.0'">
<DefineConstants>$(DefineConstants);NETWPF</DefineConstants>
<WPFSupported>true</WPFSupported>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFrameworkVersion)' == 'v2.0'">
<WPFSupported>false</WPFSupported>
</PropertyGroup>
<Choose>
<When Condition=" '$(WPFSupported)' ">
<ItemGroup>
<Reference Include="WindowsBase" />
<Reference Include="PresentationFramework" />
</ItemGroup>
</When>
</Choose>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down
121 changes: 92 additions & 29 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -67,12 +68,6 @@ public static class AutoUpdater
/// </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.
/// </summary>
Expand Down Expand Up @@ -118,6 +113,16 @@ public static void Start()
Start(AppCastURL);
}

/// <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>
Expand All @@ -133,11 +138,49 @@ public static void Start(String appCast, Assembly myAssembly = null)

backgroundWorker.DoWork += BackgroundWorkerDoWork;

backgroundWorker.RunWorkerCompleted += BackgroundWorkerOnRunWorkerCompleted;

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

private static void BackgroundWorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs)
{
if (!runWorkerCompletedEventArgs.Cancelled)
{
if (runWorkerCompletedEventArgs.Result is DateTime)
{
var remindLaterTime = (DateTime) runWorkerCompletedEventArgs.Result;
SetTimer(remindLaterTime);
}
else
{
var args = runWorkerCompletedEventArgs.Result as UpdateInfoEventArgs;
if (CheckForUpdateEvent != null)
{
CheckForUpdateEvent(args);
}
else
{
if (args != null)
{
if (args.IsUpdateAvailable)
{
if (!IsWinFormsApplication)
{
Application.EnableVisualStyles();
}
var updateForm = new UpdateForm();
updateForm.Show();
}
}
}
}
}
}

private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
e.Cancel = true;
Assembly mainAssembly = e.Argument as Assembly;

var companyAttribute =
Expand All @@ -157,7 +200,7 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)

InstalledVersion = mainAssembly.GetName().Version;

WebRequest webRequest = WebRequest.Create(AppCastURL);
var webRequest = WebRequest.Create(AppCastURL);
webRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

WebResponse webResponse;
Expand All @@ -168,7 +211,7 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
}
catch (Exception)
{
CheckForUpdateEvent?.Invoke(null);
e.Cancel = false;
return;
}

Expand All @@ -182,7 +225,7 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
}
else
{
CheckForUpdateEvent?.Invoke(null);
e.Cancel = false;
return;
}

Expand Down Expand Up @@ -276,7 +319,8 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)

if (compareResult < 0)
{
SetTimer(remindLater);
e.Cancel = false;
e.Result = remindLater;
return;
}
}
Expand All @@ -290,22 +334,11 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
ChangelogURL = ChangeLogURL,
CurrentVersion = CurrentVersion,
InstalledVersion = InstalledVersion,
IsUpdateAvailable = false
IsUpdateAvailable = CurrentVersion > InstalledVersion
};

if (CurrentVersion > InstalledVersion)
{
args.IsUpdateAvailable = true;
if (CheckForUpdateEvent == null)
{
var thread = new Thread(ShowUI);
thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
}

CheckForUpdateEvent?.Invoke(args);
e.Cancel = false;
e.Result = args;
}

private static string GetURL(Uri baseUri, XmlNode xmlNode)
Expand All @@ -325,11 +358,40 @@ private static string GetURL(Uri baseUri, XmlNode xmlNode)
return temp;
}

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

ownerForm.Close();

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

private static Attribute GetAttribute(Assembly assembly, Type attributeType)
Expand Down Expand Up @@ -360,17 +422,18 @@ internal static void SetTimer(DateTime remindLater)
/// <summary>
/// Opens the Download window that download the update and execute the installer when download completes.
/// </summary>
public static void DownloadUpdate()
public static bool DownloadUpdate()
{
var downloadDialog = new DownloadUpdateDialog(DownloadURL);

try
{
downloadDialog.ShowDialog();
return downloadDialog.ShowDialog().Equals(DialogResult.OK);
}
catch (TargetInvocationException)
{
}
return false;
}
}

Expand Down
Loading

0 comments on commit 14b8f68

Please sign in to comment.