Click here to Skip to main content
13,595,908 members
Click here to Skip to main content
Add your own
alternative version


71 bookmarked
Posted 12 Aug 2004
Licenced CPOL

MessageBox with a timeout for .NET

, 12 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Just like MessageBox.Show but with an added timeout parameter


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!


  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.


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


GipsySoft: You'll find a C++ implementation with many more features.


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralRe: Converted to C++/CLI Pin
RodgerB1-Sep-11 9:28
memberRodgerB1-Sep-11 9:28 
GeneralThank you! Pin
ChrisKane15-May-11 22:19
memberChrisKane15-May-11 22:19 
GeneralMy vote of 5 Pin
matrixology4-May-11 5:06
membermatrixology4-May-11 5:06 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pin
kirangiri4-Jan-11 7:26
memberkirangiri4-Jan-11 7:26 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pin
kirangiri4-Jan-11 7:26
memberkirangiri4-Jan-11 7:26 
GeneralMy vote of 5 Pin
Francesco Pasquesi1-Dec-10 3:10
memberFrancesco Pasquesi1-Dec-10 3:10 
GeneralSome enhancements Pin
DanielLey25-Sep-10 6:59
memberDanielLey25-Sep-10 6:59 
GeneralMy vote of 5 Pin
TomChantler22-Sep-10 4:59
memberTomChantler22-Sep-10 4:59 
GeneralThis isn't working in win7, Visual Studio 2008 Pin
Mr C P9-Mar-10 23:10
memberMr C P9-Mar-10 23:10 
GeneralRe: This isn't working in win7, Visual Studio 2008 Pin
Mr C P9-Mar-10 23:15
memberMr C P9-Mar-10 23:15 
GeneralRe: This isn't working in win7, Visual Studio 2008 Pin
TomChantler22-Sep-10 5:00
memberTomChantler22-Sep-10 5:00 
Generalgood article Pin
Donsw17-Jan-09 17:51
memberDonsw17-Jan-09 17:51 
GeneralVery very very good code, but... Pin
seb.4924-Apr-08 5:20
memberseb.4924-Apr-08 5:20 
QuestionDoes this support multi threading calls Pin
Kanna22-Mar-08 5:23
memberKanna22-Mar-08 5:23 
GeneralRe: Does this support multi threading calls Pin
RodgerB31-Mar-08 13:00
memberRodgerB31-Mar-08 13:00 
GeneralPocketPC Pin
richardjjs14-Apr-07 21:56
memberrichardjjs14-Apr-07 21:56 
GeneralRe: PocketPC [modified] Pin
RodgerB15-Apr-07 4:50
memberRodgerB15-Apr-07 4:50 
GeneralAmazing Pin
azam's13-Feb-07 18:21
memberazam's13-Feb-07 18:21 
GeneralRe: Amazing Pin
Andreas Hollmann2-May-07 0:24
memberAndreas Hollmann2-May-07 0:24 
QuestionHow to implement in CF Pin
Ajatashatru13-Sep-06 3:19
memberAjatashatru13-Sep-06 3:19 
AnswerRe: How to implement in CF Pin
RodgerB13-Sep-06 3:46
memberRodgerB13-Sep-06 3:46 
GeneralServiceNotification Pin
Ryan McFarren5-Jun-06 10:23
memberRyan McFarren5-Jun-06 10:23 
QuestionMessageBoxTimeOut API function? Pin
eshipman13-Dec-04 3:42
membereshipman13-Dec-04 3:42 
AnswerRe: MessageBoxTimeOut API function? Pin
RodgerB13-Dec-04 5:08
memberRodgerB13-Dec-04 5:08 
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pin
Anonymous13-Oct-04 12:51
sussAnonymous13-Oct-04 12:51 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 13 Aug 2004
Article Copyright 2004 by RodgerB
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid