Click here to Skip to main content
Licence CPOL
First Posted 12 Aug 2004
Views 132,674
Bookmarked 66 times

MessageBox with a timeout for .NET

By | 12 Aug 2004 | Article
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)

About the Author

RodgerB

Software Developer (Senior)
Bloomberg
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWow PinmemberWabiSabi14:52 6 Oct '11  
GeneralConverted to C++/CLI PinmemberSharjith9:07 1 Sep '11  
GeneralRe: Converted to C++/CLI PinmemberRodgerB9:28 1 Sep '11  
GeneralThank you! PinmemberChrisKane22:19 15 May '11  
GeneralMy vote of 5 Pinmembermatrixology5:06 4 May '11  
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pinmemberkirangiri7:26 4 Jan '11  
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pinmemberkirangiri7:26 4 Jan '11  
GeneralMy vote of 5 PinmemberFrancesco Pasquesi3:10 1 Dec '10  
GeneralSome enhancements PinmemberDanielLey6:59 25 Sep '10  
GeneralMy vote of 5 PinmemberTomChantler4:59 22 Sep '10  
GeneralThis isn't working in win7, Visual Studio 2008 PinmemberMr C P23:10 9 Mar '10  
GeneralRe: This isn't working in win7, Visual Studio 2008 PinmemberMr C P23:15 9 Mar '10  
GeneralRe: This isn't working in win7, Visual Studio 2008 PinmemberTomChantler5:00 22 Sep '10  
Generalgood article PinmemberDonsw17:51 17 Jan '09  
GeneralVery very very good code, but... Pinmemberseb.495:20 24 Apr '08  
QuestionDoes this support multi threading calls PinmemberKanna5:23 22 Mar '08  
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 PinmemberRodgerB13:00 31 Mar '08  
GeneralPocketPC Pinmemberrichardjjs21:56 14 Apr '07  
GeneralRe: PocketPC [modified] PinmemberRodgerB4:50 15 Apr '07  
GeneralAmazing Pinmemberazam's18:21 13 Feb '07  
GeneralRe: Amazing PinmemberAndreas Hollmann0:24 2 May '07  
QuestionHow to implement in CF PinmemberAjatashatru3:19 13 Sep '06  
AnswerRe: How to implement in CF PinmemberRodgerB3:46 13 Sep '06  
GeneralServiceNotification PinmemberRyan McFarren10:23 5 Jun '06  
QuestionMessageBoxTimeOut API function? Pinmembereshipman3:42 13 Dec '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 13 Aug 2004
Article Copyright 2004 by RodgerB
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid