Click here to Skip to main content
11,413,961 members (77,639 online)
Click here to Skip to main content

MessageBox with a timeout for .NET

, 12 Aug 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

RodgerB
Software Developer (Senior) Bloomberg
United States United States
No Biography provided

Comments and Discussions

 
QuestionHow to set icon MessageBox display on Windows Taskbar ? Pin
Member 11396325, 22-Jan-15 21:37
memberMember 1139632522-Jan-15 21:37 
AnswerRe: How to set icon MessageBox display on Windows Taskbar ? Pin
Member 10842427, 22-Jan-15 23:22
memberMember 1084242722-Jan-15 23:22 
QuestionConverting to VB Pin
Rob Peterson, 4-Mar-14 3:19
memberRob Peterson4-Mar-14 3:19 
QuestionSystem.AppDomain.GetCurrentThreadId()' is obsolete Pin
Member 10349652, 29-Oct-13 6:10
memberMember 1034965229-Oct-13 6:10 
QuestionGracias CApo! Pin
ovamendocino, 23-Feb-13 6:37
memberovamendocino23-Feb-13 6:37 
QuestionThanks Pin
vasanthkumarmk, 17-Aug-12 3:40
membervasanthkumarmk17-Aug-12 3:40 
Questionit does not work when we use servicenotification message box (please suggest something) Pin
hsubhashis, 5-Jun-12 0:20
memberhsubhashis5-Jun-12 0:20 
AnswerRe: it does not work when we use servicenotification message box (please suggest something) Pin
RodgerB, 5-Jun-12 0:34
memberRodgerB5-Jun-12 0:34 
GeneralWow Pin
WabiSabi1, 6-Oct-11 5:52
memberWabiSabi16-Oct-11 5:52 
GeneralConverted to C++/CLI Pin
Sharjith, 1-Sep-11 10:07
memberSharjith1-Sep-11 10:07 
GeneralRe: Converted to C++/CLI Pin
RodgerB, 1-Sep-11 10:28
memberRodgerB1-Sep-11 10:28 
GeneralThank you! Pin
ChrisKane, 15-May-11 23:19
memberChrisKane15-May-11 23:19 
GeneralMy vote of 5 Pin
matrixology, 4-May-11 6:06
membermatrixology4-May-11 6:06 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pin
kirangiri, 4-Jan-11 8:26
memberkirangiri4-Jan-11 8:26 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pin
kirangiri, 4-Jan-11 8:26
memberkirangiri4-Jan-11 8:26 
GeneralMy vote of 5 Pin
Francesco Pasquesi, 1-Dec-10 4:10
memberFrancesco Pasquesi1-Dec-10 4:10 
GeneralSome enhancements Pin
DanielLey, 25-Sep-10 7:59
memberDanielLey25-Sep-10 7:59 
GeneralMy vote of 5 Pin
TomChantler, 22-Sep-10 5:59
memberTomChantler22-Sep-10 5:59 
GeneralThis isn't working in win7, Visual Studio 2008 Pin
Mr C P, 10-Mar-10 0:10
memberMr C P10-Mar-10 0:10 
GeneralRe: This isn't working in win7, Visual Studio 2008 Pin
Mr C P, 10-Mar-10 0:15
memberMr C P10-Mar-10 0:15 
GeneralRe: This isn't working in win7, Visual Studio 2008 Pin
TomChantler, 22-Sep-10 6:00
memberTomChantler22-Sep-10 6:00 
Generalgood article Pin
Donsw, 17-Jan-09 18:51
memberDonsw17-Jan-09 18:51 
GeneralVery very very good code, but... Pin
seb.49, 24-Apr-08 6:20
memberseb.4924-Apr-08 6:20 
QuestionDoes this support multi threading calls Pin
Kanna, 22-Mar-08 6:23
memberKanna22-Mar-08 6:23 
If two threads calls this MessageBoxEx, would this be thread safe?
I am a bit skeptical with the timer event ID being set to a constant 42.

~Kanna

GeneralRe: Does this support multi threading calls Pin
RodgerB, 31-Mar-08 14:00
memberRodgerB31-Mar-08 14:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 13 Aug 2004
Article Copyright 2004 by RodgerB
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid