In my WPF app, I sometimes being up a System.Windows.MessageBox
. When it is initially displayed, it is shown on top of my main application window, as I would like. Is there a way that I can force it to ALWAYS remain top of the main window? The problem I have is that when a MessageBox
is displayed, users can then click on the main app window and bring it to the front, meaning the MessageBox
becomes hidden from view. In this case the user might not realize it's there, or forget about it, and to them, the main app seems to have frozen.
I've read a number of threads about this, but none have resolved the problem for me.
I ought to add that the thread putting up the MessageBox
might not be the UI thread.
Thanks
Tom
Use the version of MessageBox.Show
that takes a Window "owner" and pass your window.
MessageBox.Show(Application.Current.MainWindow, "Im always on top - of the main window");
If your possibly not on the UI thread try:
string msg="Hello!";
if (Application.Current.Dispatcher.CheckAccess()) {
MessageBox.Show(Application.Current.MainWindow, msg);
}
else {
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(()=>{
MessageBox.Show(Application.Current.MainWindow, msg);
}));
}
You can:
1. Invoke
to block your thread until MessageBox
is dismissed OR
2. BeginInvoke
in which case your thread code will continue to execute but UI thread will block on MessageBox
until its dismissed).