Popping a MessageBox for the main app with Backgroundworker in WPF

前端 未结 5 1072
-上瘾入骨i
-上瘾入骨i 2021-01-02 01:51

In a WPF app, I am using a BackgroundWorker to periodically check for a condition on the server. While that works fine, I want to pop a MessageBox notifing the users if some

5条回答
  •  别那么骄傲
    2021-01-02 02:43

    Replace

    MessageBox.Show("I should block the main window"); 
    

    with

    this.Invoke((Func)(() => MessageBox.Show("I should block the main window")));
    

    that will cause the message box to be on the main thread and will block all access to the UI until it gets a response. As a added bonus this.Invoke will return a object that can be cast in to the DialogResult.

提交回复
热议问题