MessageBox with a timeout for .NET
Just like MessageBox.Show but with an added timeout parameter
Introduction
Have you ever wanted a message box that times out? Well here you go.
Using the code
- Add MessageBoxEx.cs to your project or assembly your project references.
- Change
MessageBox.Show(...);
calls toMessageBoxEx.Show(..., timeout);
. - That's it!
Notes
MessageBoxEx
has all 12 of theMessageBox.Show
overloads.- The timeout is expressed in milliseconds.
MessageBoxEx
does not support reentrancy. Good thing too. Why would you want to have more than one message box at the same time?- If the user doesn't press a button before the timeout elapses the function will return the
DialogResult
of the messagebox dialogs default button.
How does it work?
Just before calling MessageBox.Show(...)
, SetWindowsHookEx(WH_CALLWNDPROCRET, ...)
is called. The hook proc looks for a
WM_INITDIALOG
on a window with text equal to the message box caption. A windows timer is started on that window with the appropriate timeout. When the timeout fires
EndDialog
is called with the result set to the dialog default button ID. You can get that ID by sending a dialog box a
DM_GETDEFID
message. Pretty simple.