diff --git a/PdfScribe/ActivityNotificationPresenter.cs b/PdfScribe/ActivityNotificationPresenter.cs index 84e2d89..c61ffc1 100644 --- a/PdfScribe/ActivityNotificationPresenter.cs +++ b/PdfScribe/ActivityNotificationPresenter.cs @@ -11,18 +11,38 @@ namespace PdfScribe { public class ActivityNotificationPresenter { - public ActivityNotificationPresenter() + public ActivityNotificationPresenter(Application guiApplication) { - progressTimer = new SysTimers.Timer(); - progressTimer.Enabled = false; - progressTimer.Interval = 250; // Quarter second is default - progressTimer.Elapsed += new SysTimers.ElapsedEventHandler(progressTimer_Elapsed); + this.activityWindowApp = guiApplication; + this.progressTimer = new SysTimers.Timer(); + this.progressTimer.Enabled = false; + this.progressTimer.Interval = 250; // Quarter second is default + this.progressTimer.Elapsed += new SysTimers.ElapsedEventHandler(progressTimer_Elapsed); + this.activityWindow = new ActivityNotification(); + /* + if (guiApplication.Dispatcher.CheckAccess()) + { + this.activityWindow = new ActivityNotification(); + } + else + { + guiApplication.Dispatcher.Invoke((Action)delegate() + { + this.activityWindow = new ActivityNotification(); + progressTimer = new SysTimers.Timer(); + progressTimer.Enabled = false; + progressTimer.Interval = 250; // Quarter second is default + progressTimer.Elapsed += new SysTimers.ElapsedEventHandler(progressTimer_Elapsed); + } + ); + } + */ } - private Application activityWindowApp = null; - private ActivityNotification activityWindow; + private Application activityWindowApp; + private ActivityNotification activityWindow = null; private SysTimers.Timer progressTimer; readonly String progressString = "CAPTURING"; @@ -32,6 +52,27 @@ namespace PdfScribe /// a separate thread /// public void ShowActivityNotificationWindow() + { + if (this.activityWindow != null) + { + if (this.activityWindow.Dispatcher.CheckAccess()) + { + this.activityWindow.Show(); + } + else + { + this.activityWindow.Dispatcher.Invoke((Action)delegate() + { + this.activityWindow.Show(); + } + ); + } + this.progressTimer.Start(); + } + } + + /* + public void ShowActivityNotificationWindow() { if (this.activityWindowApp == null) { @@ -48,6 +89,7 @@ namespace PdfScribe this.progressTimer.Enabled = true; } } + */ /// /// Shuts down the WPF Application showing @@ -55,6 +97,25 @@ namespace PdfScribe /// public void CloseActivityNotificationWindow() { + this.progressTimer.Enabled = false; + if (this.activityWindow != null) + { + if (this.activityWindow.Dispatcher.CheckAccess()) + { + this.activityWindow.Close(); + } + else + { + this.activityWindow.Dispatcher.Invoke((Action)delegate() + { + this.activityWindow.Close(); + } + ); + } + this.activityWindow = null; + } + + /* if (activityWindowApp != null) { this.progressTimer.Stop(); @@ -73,6 +134,7 @@ namespace PdfScribe this.progressTimer.Dispose(); this.progressTimer = null; } + */ } @@ -80,15 +142,26 @@ namespace PdfScribe private void progressTimer_Elapsed(object sender, SysTimers.ElapsedEventArgs e) { ((SysTimers.Timer)sender).Enabled = false; - activityWindowApp.Dispatcher.Invoke((Action)delegate() - { - if (this.progressCounter >= progressString.Length) - this.progressCounter = 0; - activityWindow.labelProgress.Content = progressString.Substring(0, progressCounter + 1); - progressCounter++; - } - ); - ((SysTimers.Timer)sender).Enabled = true; + if (activityWindow != null) + { + if (this.progressCounter >= progressString.Length) + this.progressCounter = 0; + + if (activityWindow.labelProgress.Dispatcher.CheckAccess()) + { + activityWindow.labelProgress.Content = progressString.Substring(0, progressCounter + 1); + } + else + { + activityWindow.labelProgress.Dispatcher.Invoke((Action)delegate() + { + activityWindow.labelProgress.Content = progressString.Substring(0, progressCounter + 1); + } + ); + } + progressCounter++; + ((SysTimers.Timer)sender).Enabled = true; + } } } diff --git a/PdfScribe/Program.cs b/PdfScribe/Program.cs index f5ac268..238fc71 100644 --- a/PdfScribe/Program.cs +++ b/PdfScribe/Program.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading; +using System.Windows; using PdfScribeCore; @@ -38,7 +39,8 @@ namespace PdfScribe #endregion - static ActivityNotificationPresenter userDisplay = new ActivityNotificationPresenter(); + static Application guiApplication = null; + static ActivityNotificationPresenter userDisplay; static TraceSource logEventSource = new TraceSource(traceSourceName); [STAThread] @@ -46,8 +48,13 @@ namespace PdfScribe { // Install the global exception handler AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException); + + // Setup and start the WPF application that will + // handle Windows and other displayables + LaunchApplication(); + userDisplay = new ActivityNotificationPresenter(guiApplication); userDisplay.ShowActivityNotificationWindow(); - Thread.Sleep(3000); + //Thread.Sleep(20000); String standardInputFilename = Path.GetTempFileName(); String outputFilename = Path.Combine(Path.GetTempPath(), defaultOutputFilename); @@ -125,6 +132,7 @@ namespace PdfScribe String.Format(warnFileNotDeleted, standardInputFilename)); } userDisplay.CloseActivityNotificationWindow(); + ShutdownApplication(); } } @@ -140,12 +148,51 @@ namespace PdfScribe { logEventSource.TraceEvent(TraceEventType.Critical, (int)TraceEventType.Critical, - ((Exception)e.ExceptionObject).Message); + ((Exception)e.ExceptionObject).Message + Environment.NewLine + + ((Exception)e.ExceptionObject).StackTrace); ErrorDialogPresenter errorDialog = new ErrorDialogPresenter(errorDialogCaption, errorDialogInstructionUnexpectedError, - String.Empty); + ((Exception)e.ExceptionObject).Message + + Environment.NewLine + + ((Exception)e.ExceptionObject).StackTrace); } + static void LaunchApplication() + { + if (guiApplication == null) + { + guiApplication = new Application(); + var guiApplicationThread = new Thread(new ThreadStart(() => + { + guiApplication.ShutdownMode = ShutdownMode.OnExplicitShutdown; + guiApplication.Run(); + } + )); + guiApplicationThread.SetApartmentState(ApartmentState.STA); + guiApplicationThread.Start(); + } + } + + static void ShutdownApplication() + { + if (guiApplication != null) + { + guiApplication.Dispatcher.Invoke((Action)delegate() + { + if (guiApplication.Windows != null && guiApplication.Windows.Count > 0) + { + foreach (Window appWindow in guiApplication.Windows) + { + appWindow.Close(); + } + } + + } + ); + guiApplication.Dispatcher.InvokeShutdown(); + guiApplication = null; + } + } } }