public void Close() { if (_launchedWindow == null) { return; } InvokeSetter <bool>(new SetterInfo <bool>("SetCanClose", true)); if (_launchedWindow.Window.Dispatcher.CheckAccess()) { _launchedWindow.Window.Close(); } else { _launchedWindow.Window.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(Close)); } _launchedWindow = null; }
public void LaunchThreadedWindow <T>(bool modal, bool cancelButton = false) where T : Window, IThreadedWindow, new() { if (_launchedWindow != null) { throw new Exception("Threaded window already launched."); } IntPtr ownerHandle = GetActiveWindow(); Thread thread = new Thread(() => { T w = new T(); _launchedWindow = w; w.Window.ShowInTaskbar = true; SetOwnerWindow(w, ownerHandle); _event.Set(); if (modal) { // ShowDialog automatically starts the event queue for the new windows in the new thread. // The window isn't modal though. w.ShowDialog(); } else { w.Show(); w.Closed += (sender2, e2) => w.Dispatcher.InvokeShutdown(); System.Windows.Threading.Dispatcher.Run(); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); _event.Wait(MaxThreadWaitMilliseconds); _event.Reset(); }
public ThreadedWindowWrapper() { _launchedWindow = null; _event = new ManualResetEventSlim(false); }