Beispiel #1
0
        public void InvokeSynchronously(SendOrPostCallback callback, object state)
        {
            ArgumentHelper.AssertNotNull(callback, "callback");
            EnsureInitialized();

            _context.Send(callback, state);
        }
        public void InvokeAndBlockUntilCompletion(SendOrPostCallback callback, object state)
        {
            ArgumentValidator.AssertNotNull(callback, "callback");
            EnsureInitialized();

            context.Send(callback, state);
        }
Beispiel #3
0
        public void InvokeSynchronously(SendOrPostCallback callback, object state)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            EnsureInitialized();

            _context.Send(callback, state);
        }
Beispiel #4
0
        object RunOnUIThread(DispatcherOperationCallback dispatcherMethod)
        {
            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(JobWindow.Dispatcher);

            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);
        }
        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();
        }
        private static void Invoke(this Dispatcher dispatcher, Action action)
        {
            Contract.Requires(dispatcher != null);
            Contract.Requires(action != null);

            var context = new DispatcherSynchronizationContext(dispatcher);

            context.Send(state =>
            {
                action();
            }, null);
        }
Beispiel #7
0
        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);
        }
        public static void ShowOnUIThread(string fileName, Dictionary <string, string> typeScriptFileNameToProgress)
        {
            Thread t = new Thread(new ThreadStart(() =>
            {
                var synchronizationContext = new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher);
                SynchronizationContext.SetSynchronizationContext(synchronizationContext);

                var progressDialog = new ProgressDialog();
                progressDialog.Show();

                Thread t2 = new Thread(new ThreadStart(() =>
                {
                    while (typeScriptFileNameToProgress[fileName] != ProgressDialog.Done)
                    {
                        synchronizationContext.Send(s =>
                        {
                            progressDialog.Text = typeScriptFileNameToProgress[fileName];
                        }, null);
                        Thread.Sleep(200);
                    }
                    synchronizationContext.Send(s =>
                    {
                        progressDialog.Hide();
                    }, null);
                }));

                t2.IsBackground = true; // Prevents the background thread from keeping the application alive.
                t2.Start();

                System.Windows.Threading.Dispatcher.Run();
            }));

            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true; // Prevents the background thread from keeping the application alive.
            t.Start();
        }
Beispiel #9
0
        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));
        }
Beispiel #10
0
        public void GoBack()
        {
            if (!Deployment.Current.CheckAccess())
            {
                var context = new DispatcherSynchronizationContext(Deployment.Current.Dispatcher);
                context.Send(_ => GoBack(), null);
                return;
            }

            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;

            Debug.Assert(root != null, "Root is null");
            if (root.CanGoBack)
            {
                root.GoBack();
            }
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        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();
        }
Beispiel #13
0
        public void NavigateTo(View view, Dictionary <string, string> parameters)
        {
            if (!Deployment.Current.CheckAccess())
            {
                var context = new DispatcherSynchronizationContext(Deployment.Current.Dispatcher);
                context.Send(_ => NavigateTo(view, parameters), null);
                return;
            }

            if (!_views.ContainsKey(view))
            {
                return;
            }

            string address = _views[view];

            StringBuilder builder = new StringBuilder(address);

            if (parameters != null && parameters.Count > 0)
            {
                builder.Append("?");
                bool prependAmp = false;
                foreach (KeyValuePair <string, string> parameterPair in parameters)
                {
                    if (prependAmp)
                    {
                        builder.Append("&");
                    }
                    builder.AppendFormat("{0}={1}", parameterPair.Key, parameterPair.Value);
                    prependAmp = true;
                }
            }

            address = builder.ToString();

            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;

            Debug.Assert(root != null, "Root is null");
            root.Navigate(new Uri(address, UriKind.Relative));
        }
Beispiel #14
0
 public void Dispatch(Action action)
 {
     _context.Send(state => action.Invoke(), null);
 }