Popping a MessageBox for the main app with Backgroundworker in WPF

前端 未结 5 1049
-上瘾入骨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:46

    It doesn't only not block the main window, it is also very likely to disappear behind it. That's a direct consequence of it running on a different thread. When you don't specify an owner for the message box then it goes looking for one with the GetActiveWindow() API function. It only considers windows that use the same message queue. Which is a thread-specific property. Losing a message box is quite hard to deal with of course.

    Similarly, MessageBox only disables windows that belong to the same message queue. And thus won't block windows created by your main thread.

    Fix your problem by letting the UI thread display the message box. Use Dispatcher.Invoke or leverage either the ReportProgress or RunWorkerCompleted events. Doesn't sound like the events are appropriate here.

提交回复
热议问题