WPF跨线程MessageBox不阻塞主线程输入

J3s*_*oon 6 c# wpf multithreading

我试图表现出MessageBox从另一个Thread使用Dispatcher.Invoke.

MessageBox没有露面.但是没有点击OK按钮MessageBox,我仍然可以控制主窗口.

我在想是否有办法在点击OK按钮之前阻止主窗口输入?

任何帮助表示赞赏.

我到目前为止的代码:

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);
            }));
    }
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*ain 8

保证消息框阻止窗口的唯一方法是将窗口传递到消息框.

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);
            }));
    }
}
Run Code Online (Sandbox Code Playgroud)