Click here to Skip to main content
11,414,234 members (69,738 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

 
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 
Great code.
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 
GeneralRe: Does this support multi threading calls Pin
RodgerB, 31-Mar-08 14:00
memberRodgerB31-Mar-08 14:00 
GeneralPocketPC Pin
richardjjs, 14-Apr-07 22:56
memberrichardjjs14-Apr-07 22:56 
GeneralRe: PocketPC [modified] Pin
RodgerB, 15-Apr-07 5:50
memberRodgerB15-Apr-07 5:50 
GeneralAmazing Pin
azam's, 13-Feb-07 19:21
memberazam's13-Feb-07 19:21 
GeneralRe: Amazing Pin
Andreas Hollmann, 2-May-07 1:24
memberAndreas Hollmann2-May-07 1:24 
QuestionHow to implement in CF Pin
Ajatashatru, 13-Sep-06 4:19
memberAjatashatru13-Sep-06 4:19 
AnswerRe: How to implement in CF Pin
RodgerB, 13-Sep-06 4:46
memberRodgerB13-Sep-06 4:46 
GeneralServiceNotification Pin
Ryan McFarren, 5-Jun-06 11:23
memberRyan McFarren5-Jun-06 11:23 
QuestionMessageBoxTimeOut API function? Pin
eshipman, 13-Dec-04 4:42
membereshipman13-Dec-04 4:42 
AnswerRe: MessageBoxTimeOut API function? Pin
RodgerB, 13-Dec-04 6:08
memberRodgerB13-Dec-04 6:08 
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Anonymous, 13-Oct-04 13:51
sussAnonymous13-Oct-04 13:51 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
RodgerB, 13-Oct-04 17:17
memberRodgerB13-Oct-04 17:17 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Anonymous, 13-Oct-04 18:22
sussAnonymous13-Oct-04 18:22 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Tim McCurdy, 7-Dec-04 2:56
memberTim McCurdy7-Dec-04 2:56 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Dan Ganiere, 30-Aug-05 6:13
memberDan Ganiere30-Aug-05 6:13 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
J Whattam, 4-May-06 20:36
memberJ Whattam4-May-06 20:36 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Janis Ozo, 11-Feb-07 0:21
memberJanis Ozo11-Feb-07 0:21 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
sgissinger, 24-Mar-11 3:54
membersgissinger24-Mar-11 3:54 
GeneralThanks a lot Pin
aemmons, 6-Oct-04 10:54
memberaemmons6-Oct-04 10:54 
Generalpicture Pin
Taha Zayed, 24-Sep-04 16:55
memberTaha Zayed24-Sep-04 16:55 
GeneralRe: picture Pin
RodgerB, 25-Sep-04 0:51
memberRodgerB25-Sep-04 0:51 
GeneralNamespace Errors when trying to build Pin
LordRhys, 3-Sep-04 7:49
memberLordRhys3-Sep-04 7:49 
GeneralRe: Namespace Errors when trying to build Pin
RodgerB, 3-Sep-04 8:00
memberRodgerB3-Sep-04 8:00 
GeneralRe: Namespace Errors when trying to build Pin
LordRhys, 3-Sep-04 8:38
memberLordRhys3-Sep-04 8:38 
GeneralYou beat me to it. Pin
Ashaman, 13-Aug-04 3:54
memberAshaman13-Aug-04 3:54 
GeneralRe: You beat me to it. Pin
Manolo Herrera, 13-Aug-04 6:32
sussManolo Herrera13-Aug-04 6:32 
GeneralRe: You beat me to it. Pin
Daniel Steuernol, 14-Aug-04 7:25
memberDaniel Steuernol14-Aug-04 7:25 
GeneralRe: You beat me to it. Pin
Manolo Herrera, 16-Aug-04 5:26
sussManolo Herrera16-Aug-04 5:26 

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
Web04 | 2.8.150427.2 | Last Updated 13 Aug 2004
Article Copyright 2004 by RodgerB
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid