/// <summary> /// Push an execution frame. /// </summary> /// <param name="frame"> /// The frame for the dispatcher to process. /// </param> public static void PushFrame(DispatcherFrame frame) { if (frame == null) { throw new ArgumentNullException("frame"); } Dispatcher dispatcher = Dispatcher.CurrentDispatcher; if (dispatcher._hasShutdownFinished) { throw new InvalidOperationException(); } dispatcher.PushFrameImpl(frame); }
private void OnButtonUp(object sender, ButtonEventArgs e) { // Show a modal dialog window // Create and show new window SecondWindow wnd = new SecondWindow(); wnd.Visibility = Visibility.Visible; wnd.Topmost = true; // move on top of all others DispatcherFrame modalFrame = new DispatcherFrame(); // Create a handler that will terminate the loop when the window will be hidden PropertyChangedEventHandler handler = delegate { if (wnd.Visibility != Visibility.Visible) // when the windows was hidden modalFrame.Continue = false; // tell the frame to exit the loop }; wnd.IsVisibleChanged += handler; // add handler to terminate the loop Dispatcher.PushFrame(modalFrame); // start the loop and block the calling thread // Here we go when the modal dialog was closed wnd.IsVisibleChanged -= handler; // remove handler }
// // instance implementation of PushFrame private void PushFrameImpl(DispatcherFrame frame) { _frameDepth++; try { while (frame.Continue) { DispatcherOperation op = null; bool aborted = false; // // Dequeue the next operation if appropriate if (_queue.Count > 0) { op = (DispatcherOperation)_queue.Dequeue(); //Must check aborted flag inside lock because //user program could call op.Abort() between //here and before the call to Invoke() aborted = op.Status == DispatcherOperationStatus.Aborted; } if (op != null) { if (!aborted) { // Invoke the operation: Debug.Assert(op._status == DispatcherOperationStatus.Pending); // Mark this operation as executing. op._status = DispatcherOperationStatus.Executing; op._result = null; try { op._result = op._method(op._args); } catch (Exception e) { if (_finalExceptionHandler == null || !_finalExceptionHandler(op, e)) { throw; } } // Mark this operation as completed. op._status = DispatcherOperationStatus.Completed; // Raise the Completed so anyone who is waiting will wake up. op.OnCompleted(); } } else { _event.WaitOne(); } } } finally { _frameDepth--; // If this was the last frame to exit after a quit, we // can now dispose the dispatcher. if (_frameDepth == 0) { if (_hasShutdownStarted) { ShutdownImpl(); } } } }
public MessageBox(Font font, int width, string message, MessageBoxButton buttonsSet, Bitmap buttonBackground, Color buttonTextColor, Color textColor) { Background = new SolidColorBrush(Colors.Black); Width = width; Visibility = Visibility.Hidden; SizeToContent = SizeToContent.Height; this.buttonsSet = buttonsSet; main = Application.Current.MainWindow; modalBlock = new DispatcherFrame(); StackPanel pnl = new StackPanel(Orientation.Vertical); Child = pnl; txtMessage = new Text(font, message); txtMessage.ForeColor = textColor; txtMessage.HorizontalAlignment = HorizontalAlignment.Center; txtMessage.TextAlignment = TextAlignment.Center; txtMessage.TextWrap = true; txtMessage.SetMargin(3); pnl.Children.Add(txtMessage); StackPanel pnlButtons = new StackPanel(Orientation.Horizontal); pnlButtons.HorizontalAlignment = HorizontalAlignment.Center; pnlButtons.VerticalAlignment = VerticalAlignment.Bottom; pnl.Children.Add(pnlButtons); btn1 = new Button(font, "", null, buttonTextColor); btn1.Background = buttonBackground; btn1.ImageSize = 16; btn1.HorizontalAlignment = HorizontalAlignment.Center; btn1.SetMargin(3); btn1.Clicked += new EventHandler(btn1_Clicked); pnlButtons.Children.Add(btn1); btn2 = new Button(font, "", null, buttonTextColor); btn2.Background = buttonBackground; btn2.ImageSize = 16; btn2.HorizontalAlignment = HorizontalAlignment.Center; btn2.SetMargin(3); btn2.Clicked += new EventHandler(btn2_Clicked); pnlButtons.Children.Add(btn2); btn3 = new Button(font, "", null, buttonTextColor); btn3.Background = buttonBackground; btn3.ImageSize = 16; btn3.HorizontalAlignment = HorizontalAlignment.Center; btn3.SetMargin(3); btn3.Clicked += new EventHandler(btn3_Clicked); pnlButtons.Children.Add(btn3); switch (buttonsSet) { case MessageBoxButton.OK: btn1.Text = "OK"; btn2.Visibility = btn3.Visibility = Visibility.Collapsed; break; case MessageBoxButton.OKCancel: btn1.Text = "OK"; btn2.Text = "Отмена"; btn3.Visibility = Visibility.Collapsed; break; case MessageBoxButton.YesNo: btn1.Text = "Да"; btn2.Text = "Нет"; btn3.Visibility = Visibility.Collapsed; break; case MessageBoxButton.YesNoCancel: btn1.Text = "Да"; btn2.Text = "Нет"; btn3.Text = "Отмена"; break; } //Top = (SystemMetrics.ScreenHeight - Height) / 2; }