WPF Cross-Thread MessageBox does not block main thread input

I am trying to show MessageBoxfrom another Threadusing Dispatcher.Invoke.

Has appeared MessageBox. But without clicking OKon a button , MessageBoxI can still control the main window.

I think if there is a way to block the input of the main window before clicking the button OK?

Any help is appreciated.

The code I still have:

void ShowError(String message)  //I call this function in another thread
{
    if (this.Dispatcher.CheckAccess())
        MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
    else
    {
        this.Dispatcher.Invoke(
            new Action(() =>
            {
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }));
    }
}
+4
source share
1 answer

The only way to ensure that the message box is blocking the window is by passing the window to the message box.

void ShowError(String message, Window windowToBlock)  //I call this function in another thread
{
    if (this.Dispatcher.CheckAccess())
        MessageBox.Show(windowToBlock, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
    else
    {
        this.Dispatcher.Invoke(
            new Action(() =>
            {
                MessageBox.Show(windowToBlock, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }));
    }
}
+6
source

Source: https://habr.com/ru/post/1625524/


All Articles