11using System ;
22using System . ComponentModel ;
33using System . Diagnostics ;
4- using System . Drawing ;
54using System . Globalization ;
65using System . IO ;
76using System . Net ;
87using System . Net . Cache ;
98using System . Reflection ;
109using System . Threading ;
10+ using System . Windows ;
1111using System . Windows . Forms ;
1212using System . Xml ;
1313using System . Xml . Serialization ;
1414using AutoUpdaterDotNET . Properties ;
15+ using Application = System . Windows . Forms . Application ;
16+ using MessageBox = System . Windows . Forms . MessageBox ;
17+ using Size = System . Drawing . Size ;
1518using Timer = System . Timers . Timer ;
1619
1720namespace AutoUpdaterDotNET ;
@@ -86,6 +89,8 @@ public static class AutoUpdater
8689
8790 private static bool _isWinFormsApplication ;
8891
92+ private static IWin32Window _owner ;
93+
8994 private static Timer _remindLaterTimer ;
9095
9196 internal static Uri BaseUri ;
@@ -243,6 +248,20 @@ public static class AutoUpdater
243248 /// </summary>
244249 public static event ParseUpdateInfoHandler ParseUpdateInfoEvent ;
245250
251+ /// <summary>
252+ /// Set the owner for all dialogs.
253+ /// </summary>
254+ /// <param name="obj">WPF Window or Windows Form object to be used as owner for all dialogs.</param>
255+ public static void SetOwner ( object obj )
256+ {
257+ _owner = obj switch
258+ {
259+ Form form => form ,
260+ Window window => new Wpf32Window ( window ) ,
261+ _ => _owner
262+ } ;
263+ }
264+
246265 /// <summary>
247266 /// Start checking for new version of application and display a dialog to the user if update is available.
248267 /// </summary>
@@ -503,7 +522,8 @@ private static bool StartUpdate(object result)
503522
504523 if ( ReportErrors )
505524 {
506- MessageBox . Show ( Resources . UpdateUnavailableMessage ,
525+ MessageBox . Show ( _owner ,
526+ Resources . UpdateUnavailableMessage ,
507527 Resources . UpdateUnavailableCaption ,
508528 MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
509529 }
@@ -525,15 +545,17 @@ private static void ShowError(Exception exception)
525545 {
526546 if ( exception is WebException )
527547 {
528- MessageBox . Show (
548+ MessageBox . Show ( _owner ,
529549 Resources . UpdateCheckFailedMessage ,
530- Resources . UpdateCheckFailedCaption , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
550+ Resources . UpdateCheckFailedCaption ,
551+ MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
531552 }
532553 else
533554 {
534- MessageBox . Show ( exception . Message ,
535- exception . GetType ( ) . ToString ( ) , MessageBoxButtons . OK ,
536- MessageBoxIcon . Error ) ;
555+ MessageBox . Show ( _owner ,
556+ exception . Message ,
557+ exception . GetType ( ) . ToString ( ) ,
558+ MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
537559 }
538560 }
539561 }
@@ -662,7 +684,7 @@ public static bool DownloadUpdate(UpdateInfoEventArgs args)
662684
663685 try
664686 {
665- return downloadDialog . ShowDialog ( ) . Equals ( DialogResult . OK ) ;
687+ return downloadDialog . ShowDialog ( _owner ) . Equals ( DialogResult . OK ) ;
666688 }
667689 catch ( TargetInvocationException )
668690 {
@@ -684,7 +706,7 @@ public static void ShowUpdateForm(UpdateInfoEventArgs args)
684706 updateForm . Size = UpdateFormSize . Value ;
685707 }
686708
687- if ( updateForm . ShowDialog ( ) . Equals ( DialogResult . OK ) )
709+ if ( updateForm . ShowDialog ( _owner ) . Equals ( DialogResult . OK ) )
688710 {
689711 Exit ( ) ;
690712 }
0 commit comments