Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

MessageBox with a timeout for .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (53 votes)
12 Aug 2004CPOL1 min read 265K   12.5K   72   52
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

  1. Add MessageBoxEx.cs to your project or assembly your project references.
  2. Change MessageBox.Show(...); calls to MessageBoxEx.Show(..., timeout);.
  3. That's it!

Notes

  1. MessageBoxEx has all 12 of the MessageBox.Show overloads.
  2. The timeout is expressed in milliseconds.
  3. MessageBoxEx does not support reentrancy. Good thing too. Why would you want to have more than one message box at the same time?
  4. 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.

References

KB318804: HOW TO: Set a Windows Hook in Visual C#.NET

Acknowledgements

GipsySoft: http://www.gipsysoft.com/messagebox. You'll find a C++ implementation with many more features.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Adobe, LLC
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Does this support multi threading calls Pin
RodgerB31-Mar-08 13:00
RodgerB31-Mar-08 13:00 
GeneralPocketPC Pin
richardjjs14-Apr-07 21:56
richardjjs14-Apr-07 21:56 
GeneralRe: PocketPC [modified] Pin
RodgerB15-Apr-07 4:50
RodgerB15-Apr-07 4:50 
GeneralAmazing Pin
azam's13-Feb-07 18:21
azam's13-Feb-07 18:21 
GeneralRe: Amazing Pin
Andreas Hollmann2-May-07 0:24
Andreas Hollmann2-May-07 0:24 
QuestionHow to implement in CF Pin
Ajatashatru13-Sep-06 3:19
Ajatashatru13-Sep-06 3:19 
AnswerRe: How to implement in CF Pin
RodgerB13-Sep-06 3:46
RodgerB13-Sep-06 3:46 
GeneralServiceNotification Pin
Ryan McFarren5-Jun-06 10:23
Ryan McFarren5-Jun-06 10:23 
QuestionMessageBoxTimeOut API function? Pin
eshipman13-Dec-04 3:42
eshipman13-Dec-04 3:42 
AnswerRe: MessageBoxTimeOut API function? Pin
RodgerB13-Dec-04 5:08
RodgerB13-Dec-04 5:08 
Very interesting.

1) I had no idea there was such a function.
2) It's undocumented, it's generally not a good idea to use undocumented functions. Still, I would think you could try to use the undocumented version and if it's not there you could use this code.
3) Looking at another article on this site it appears that MessageBoxTimeOut is only available on WinXP. People do still write code for Win9X, WinNT, etc...
4) This technique for making a MessageBox with a timeout allows you do change the text of the buttons or move them around or do whatever else you need to do with MessageBox. The C++ code at gipsysoft does all of these things and more. I only needed the timeout so that's all I ported to C#.
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Anonymous13-Oct-04 12:51
Anonymous13-Oct-04 12:51 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
RodgerB13-Oct-04 16:17
RodgerB13-Oct-04 16:17 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Anonymous13-Oct-04 17:22
Anonymous13-Oct-04 17:22 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Tim McCurdy7-Dec-04 1:56
Tim McCurdy7-Dec-04 1:56 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Dan Ganiere30-Aug-05 5:13
Dan Ganiere30-Aug-05 5:13 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
J Whattam4-May-06 19:36
J Whattam4-May-06 19:36 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Janis Ozo10-Feb-07 23:21
Janis Ozo10-Feb-07 23:21 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
sgissinger24-Mar-11 2:54
sgissinger24-Mar-11 2:54 
GeneralThanks a lot Pin
aemmons6-Oct-04 9:54
aemmons6-Oct-04 9:54 
Generalpicture Pin
Taha Elsayed24-Sep-04 15:55
Taha Elsayed24-Sep-04 15:55 
GeneralRe: picture Pin
RodgerB24-Sep-04 23:51
RodgerB24-Sep-04 23:51 
GeneralNamespace Errors when trying to build Pin
LordRhys3-Sep-04 6:49
LordRhys3-Sep-04 6:49 
GeneralRe: Namespace Errors when trying to build Pin
RodgerB3-Sep-04 7:00
RodgerB3-Sep-04 7:00 
GeneralRe: Namespace Errors when trying to build Pin
LordRhys3-Sep-04 7:38
LordRhys3-Sep-04 7:38 
GeneralYou beat me to it. Pin
Ashaman13-Aug-04 2:54
Ashaman13-Aug-04 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.