MessageBox.Show in UI Thread

Viewed 64
this.Dispatcher.InvokeAsync(
    () => {
              MessageBoxResult result = MessageBox.Show("111111111111111111111111", "Child process failed", MessageBoxButton.OK);
          }
);

this.Dispatcher.InvokeAsync(
    () => {
              MessageBoxResult result = MessageBox.Show("22222222222222222222222", "Child process failed", MessageBoxButton.OK);
          }
);

Why does it show 2222222222 first and then show 11111111111?

If I change this.Dispatcher.InvokeAsync to this.Dispatcher.Invoke the order will be right. Does anyone know why?.

1 Answers

Dispatcher.InvokeAsync is essentially putting a message on the message queue, asking for the method to be run when the UI thread is free.

While the messagebox is showing some messages will still be processed, and this message is probably one of them. So the first messagebox will be displayed first, and a moment later the second one, on top of the first. When you click away the second messagebox it looks like the first one appears.

this.Dispatcher.Invoke is also putting a message on the message queue, but in this case the caller is also waiting for the UI thread to process the message before continuing, so the messageboxes are shown one after the other and not on top.

Related