public void Send () { DispatcherSynchronizationContext dsc = new DispatcherSynchronizationContext (); // SL throw an NRE while moonlight does not #if false Assert.Throws<NullReferenceException> (delegate { dsc.Send (null, this); }, "Send(null,object)"); #endif bool complete = false; dsc.Send (delegate (object obj) { Assert.IsNull (obj, "Send"); complete = true; }, null); EnqueueConditional (() => complete); EnqueueTestComplete (); }
object RunOnUIThread(DispatcherOperationCallback dispatcherMethod, bool async) { if (Application.Current != null) { if (Application.Current.Dispatcher.Thread == Thread.CurrentThread) { // This avoids dispatching to the UI thread if we are already in the UI thread. // Without this runing a command like 1/0 was throwing due to nested dispatches. return dispatcherMethod.Invoke(null); } } Exception e = null; object returnValue = null; SynchronizationContext sync = new DispatcherSynchronizationContext(Dispatcher); if (sync == null) { return null; } if (async) { sync.Post( new SendOrPostCallback(delegate(object obj) { try { returnValue = dispatcherMethod.Invoke(obj); } catch (Exception uiException) { e = uiException; } }), null); } else { sync.Send( new SendOrPostCallback(delegate(object obj) { try { returnValue = dispatcherMethod.Invoke(obj); } catch (Exception uiException) { e = uiException; } }), null); } if (e != null) { throw new System.Reflection.TargetInvocationException(e.Message, e); } return returnValue; }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { if (Application.Current == null) { throw new InvalidOperationException("Must have an application"); } DispatcherSynchronizationContext syncContext = new DispatcherSynchronizationContext(Application.Current.Dispatcher); object dataContext = context.DataContext; Action showDelegate = () => syncContext.Send(Show, dataContext); context.UserState = showDelegate; return showDelegate.BeginInvoke(callback, state); }
protected override void OnStartup(StartupEventArgs e) { // run bootstrapper Bootstrapper bootstrapper = new Bootstrapper(); bootstrapper.Run(); // set shut down mode ShutdownMode = ShutdownMode.OnExplicitShutdown; MainContext = (DispatcherSynchronizationContext)DispatcherSynchronizationContext.Current; MainWindow.Closing += (sender, args) => MainContext.Send(o => Shutdown(), null); // handle exceptions DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException); base.OnStartup(e); MainWindow.Show(); }
public static bool Show(Exception exception, object owner = null) { var depObj = owner as DependencyObject; var result = false; SendOrPostCallback action = obj => { var w = new ExceptionDialog((Exception)obj); try { w.Owner = depObj == null ? Application.Current.MainWindow : GetWindow(depObj); } catch (InvalidOperationException) { } result = w.ShowDialog() == true; }; var context = new DispatcherSynchronizationContext(Application.Current.Dispatcher); context.Send(action, exception); return result; }
public static bool? Show( string message, string header, MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage image = MessageBoxImage.Asterisk, object owner = null) { switch (button) { case MessageBoxButton.OK: SystemSounds.Asterisk.Play(); break; case MessageBoxButton.OKCancel: case MessageBoxButton.YesNo: case MessageBoxButton.YesNoCancel: SystemSounds.Exclamation.Play(); break; } bool? result = null; SendOrPostCallback action = obj => { var depObj = owner as DependencyObject; var w = new MessageDialog(message, header, button, image); var windowOwner = depObj == null ? Application.Current.MainWindow : GetWindow(depObj); try { w.Owner = windowOwner; } catch (InvalidOperationException) { } var view = owner as IView; if (view != null && view.ImageSource != null) w.Icon = view.ImageSource; else if (owner != null && windowOwner != null) w.Icon = windowOwner.Icon; w.ShowDialog(); result = w._result; }; var context = new DispatcherSynchronizationContext(Application.Current.Dispatcher); context.Send(action, null); return result; }