Click here to Skip to main content
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

 
Generalgood article PinmemberDonsw17-Jan-09 18:51 
GeneralVery very very good code, but... Pinmemberseb.4924-Apr-08 6:20 
QuestionDoes this support multi threading calls PinmemberKanna22-Mar-08 6:23 
GeneralRe: Does this support multi threading calls PinmemberRodgerB31-Mar-08 14:00 
GeneralPocketPC Pinmemberrichardjjs14-Apr-07 22:56 
This looks brilliant but I am not sure it will work in PocketPC(2003)
 
I am getting 9 error messages the first of which is
The type or namesapce name MessageBoxoptions could not be found (are you missin etc...
The type or namespace nane IWin32Windows etc (eight messages based on this!!
 
Richard
GeneralRe: PocketPC [modified] PinmemberRodgerB15-Apr-07 5:50 
GeneralAmazing Pinmemberazam's13-Feb-07 19:21 
GeneralRe: Amazing PinmemberAndreas Hollmann2-May-07 1:24 
QuestionHow to implement in CF PinmemberAjatashatru13-Sep-06 4:19 
AnswerRe: How to implement in CF PinmemberRodgerB13-Sep-06 4:46 
GeneralServiceNotification PinmemberRyan McFarren5-Jun-06 11:23 
QuestionMessageBoxTimeOut API function? Pinmembereshipman13-Dec-04 4:42 
AnswerRe: MessageBoxTimeOut API function? PinmemberRodgerB13-Dec-04 6:08 
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinsussAnonymous13-Oct-04 13:51 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberRodgerB13-Oct-04 17:17 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinsussAnonymous13-Oct-04 18:22 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberTim McCurdy7-Dec-04 2:56 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberDan Ganiere30-Aug-05 6:13 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberJ Whattam4-May-06 20:36 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberJanis Ozo11-Feb-07 0:21 
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 Pinmembersgissinger24-Mar-11 3:54 
GeneralThanks a lot Pinmemberaemmons6-Oct-04 10:54 
Generalpicture PinmemberTaha Zayed24-Sep-04 16:55 
GeneralRe: picture PinmemberRodgerB25-Sep-04 0:51 
GeneralNamespace Errors when trying to build PinmemberLordRhys3-Sep-04 7:49 

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