Skip to content

Commit

Permalink
Added option to set WPF Window or Windows Form as owner of all dialog…
Browse files Browse the repository at this point in the history
…s initiated by AutoUpdater. (#617)

This fixes #584.
  • Loading branch information
Fruchtzwerg94 authored and ravibpatel committed May 14, 2023
1 parent 4c34c0a commit 743dbe4
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 24 deletions.
2 changes: 1 addition & 1 deletion AutoUpdater.NET/AutoUpdater.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
<PackageReference Include="Resource.Embedder" Version="1.2.8" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.45"/>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1774.30"/>
</ItemGroup>
</Project>
40 changes: 31 additions & 9 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using AutoUpdaterDotNET.Properties;
using Application = System.Windows.Forms.Application;
using MessageBox = System.Windows.Forms.MessageBox;
using Size = System.Drawing.Size;
using Timer = System.Timers.Timer;

namespace AutoUpdaterDotNET;
Expand Down Expand Up @@ -86,6 +89,8 @@ public static class AutoUpdater

private static bool _isWinFormsApplication;

private static IWin32Window _owner;

private static Timer _remindLaterTimer;

internal static Uri BaseUri;
Expand Down Expand Up @@ -243,6 +248,20 @@ public static class AutoUpdater
/// </summary>
public static event ParseUpdateInfoHandler ParseUpdateInfoEvent;

/// <summary>
/// Set the owner for all dialogs.
/// </summary>
/// <param name="obj">WPF Window or Windows Form object to be used as owner for all dialogs.</param>
public static void SetOwner(object obj)
{
_owner = obj switch
{
Form form => form,
Window window => new Wpf32Window(window),
_ => _owner
};
}

/// <summary>
/// Start checking for new version of application and display a dialog to the user if update is available.
/// </summary>
Expand Down Expand Up @@ -503,7 +522,8 @@ private static bool StartUpdate(object result)

if (ReportErrors)
{
MessageBox.Show(Resources.UpdateUnavailableMessage,
MessageBox.Show(_owner,
Resources.UpdateUnavailableMessage,
Resources.UpdateUnavailableCaption,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Expand All @@ -525,15 +545,17 @@ private static void ShowError(Exception exception)
{
if (exception is WebException)
{
MessageBox.Show(
MessageBox.Show(_owner,
Resources.UpdateCheckFailedMessage,
Resources.UpdateCheckFailedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
Resources.UpdateCheckFailedCaption,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(exception.Message,
exception.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
MessageBox.Show(_owner,
exception.Message,
exception.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Expand Down Expand Up @@ -662,7 +684,7 @@ public static bool DownloadUpdate(UpdateInfoEventArgs args)

try
{
return downloadDialog.ShowDialog().Equals(DialogResult.OK);
return downloadDialog.ShowDialog(_owner).Equals(DialogResult.OK);
}
catch (TargetInvocationException)
{
Expand All @@ -684,7 +706,7 @@ public static void ShowUpdateForm(UpdateInfoEventArgs args)
updateForm.Size = UpdateFormSize.Value;
}

if (updateForm.ShowDialog().Equals(DialogResult.OK))
if (updateForm.ShowDialog(_owner).Equals(DialogResult.OK))
{
Exit();
}
Expand Down
2 changes: 1 addition & 1 deletion AutoUpdater.NET/DownloadUpdateDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEvent
}
catch (Exception e)
{
MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(this, e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
_webClient = null;
}
finally
Expand Down
4 changes: 2 additions & 2 deletions AutoUpdater.NET/UpdateForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void WebView_CoreWebView2InitializationCompleted(object sender,
{
if (AutoUpdater.ReportErrors)
{
MessageBox.Show(e.InitializationException.Message, e.InitializationException.GetType().ToString(),
MessageBox.Show(this, e.InitializationException.Message, e.InitializationException.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Expand Down Expand Up @@ -194,7 +194,7 @@ private void ButtonRemindLaterClick(object sender, EventArgs e)
if (AutoUpdater.LetUserSelectRemindLater)
{
using var remindLaterForm = new RemindLaterForm();
DialogResult dialogResult = remindLaterForm.ShowDialog();
DialogResult dialogResult = remindLaterForm.ShowDialog(this);

switch (dialogResult)
{
Expand Down
16 changes: 16 additions & 0 deletions AutoUpdater.NET/Wpf32Window.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Windows;
using System.Windows.Interop;
using IWin32Window = System.Windows.Forms.IWin32Window;

namespace AutoUpdaterDotNET;

internal class Wpf32Window : IWin32Window
{
public Wpf32Window(Window wpfWindow)
{
Handle = new WindowInteropHelper(wpfWindow).EnsureHandle();
}

public IntPtr Handle { get; }
}
10 changes: 5 additions & 5 deletions AutoUpdater.NET/build/Autoupdater.NET.Official.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Autoupdater.NET.Official</id>
<version>1.8.1.0</version>
<version>1.8.2.0</version>
<title>AutoUpdater.NET</title>
<authors>rbsoft</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand All @@ -17,16 +17,16 @@
<tags>autoupdate updater c# vb wpf winforms</tags>
<dependencies>
<group targetFramework=".NETFramework4.5">
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
</group>
<group targetFramework=".NETCoreApp3.1">
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
</group>
<group targetFramework="net5.0-windows7.0">
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
</group>
<group targetFramework="net6.0-windows7.0">
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
</group>
</dependencies>
<frameworkReferences>
Expand Down
4 changes: 2 additions & 2 deletions AutoUpdaterTest/AutoUpdaterTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AutoUpdater.NET\AutoUpdater.NET.csproj" />
<ProjectReference Include="..\AutoUpdater.NET\AutoUpdater.NET.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions AutoUpdaterTest/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Topmost="True"
Title="MainWindow" Height="200" Width="400" ResizeMode="NoResize">
<Grid Row="2" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
Expand Down
3 changes: 3 additions & 0 deletions AutoUpdaterTest/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ private void ButtonCheckForUpdate_Click(object sender, RoutedEventArgs e)
// Uncomment following line if you want to execute different executable after the update. This only works when you use zip file as an update file.
AutoUpdater.ExecutablePath = "bin/AutoUpdaterTest.exe";

// Uncomment following line to set this window as owner of the all dialogs initiated by AutoUpdater. It is necessary to do this if TopMost is set to true in your form or window.
AutoUpdater.SetOwner(this);

AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml");
}
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ You can specify the size of the update form by using below code.
AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);
````

### Set the owner Form / Window

To ensure the dialogs showed by the auto updater are visible and always focussed correctly related to an application
Form or Window, it may be necessary to set an owner. You can assign a Form or WPF Window as the owner by following the
example below.

````csharp
AutoUpdater.SetOwner(yourMainFormOrWpfWindow);
````

### Change storage method of Remind Later and Skip options

You can change how AutoUpdater.NET saves the Remind Later and Skip values by assigning the PersistenceProvider. If you
Expand Down
4 changes: 2 additions & 2 deletions ZipExtractor/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void FormMain_Shown(object sender, EventArgs e)

foreach (Process lockingProcess in lockingProcesses)
{
DialogResult dialogResult = MessageBox.Show(
DialogResult dialogResult = MessageBox.Show(this,
string.Format(Resources.FileStillInUseMessage,
lockingProcess.ProcessName, filePath),
Resources.FileStillInUseCaption,
Expand Down Expand Up @@ -306,7 +306,7 @@ private void FormMain_Shown(object sender, EventArgs e)
_logBuilder.AppendLine();
_logBuilder.AppendLine(exception.ToString());

MessageBox.Show(exception.Message, exception.GetType().ToString(),
MessageBox.Show(this, exception.Message, exception.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 1.8.1.{build}
version: 1.8.2.{build}
environment:
my_version: 1.8.1
my_version: 1.8.2
my_secret:
secure: vbPRaZLQYpGPr4BrZZ4p6TofpSZMud+FKtlpqjgO8aA=
skip_branch_with_pr: true
Expand Down

0 comments on commit 743dbe4

Please sign in to comment.