Click here to Skip to main content
Click here to Skip to main content

MessageBox with a timeout for .NET

By , 12 Aug 2004
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionit does not work when we use servicenotification message box (please suggest something) Pinmemberhsubhashis4 Jun '12 - 23:20 
GeneralWow PinmemberWabiSabi16 Oct '11 - 4:52 
GeneralConverted to C++/CLI PinmemberSharjith1 Sep '11 - 9:07 
GeneralThank you! PinmemberChrisKane15 May '11 - 22:19 
GeneralMy vote of 5 Pinmembermatrixology4 May '11 - 5:06 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pinmemberkirangiri4 Jan '11 - 7:26 
GeneralIts not working with DefaultDesktopOnly/ ServiceNotification option Pinmemberkirangiri4 Jan '11 - 7:26 
GeneralMy vote of 5 PinmemberFrancesco Pasquesi1 Dec '10 - 3:10 
GeneralSome enhancements PinmemberDanielLey25 Sep '10 - 6:59 
GeneralMy vote of 5 PinmemberTomChantler22 Sep '10 - 4:59 
GeneralThis isn't working in win7, Visual Studio 2008 PinmemberMr C P9 Mar '10 - 23:10 
Hullo
 
At the moment I can't seem to get this working in Windows 7, Visual Studio 2008. The messagebox appears but never times out.
 
It may be a security issue, it may also be the line I changed to correct the deprecated thread ID error:
 
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, System.Threading.Thread.CurrentThread.ManagedThreadId);
 
..perhaps it's not getting the right ID so the hook can't call EndDialog on the right window.
 
I'll report back if I get a fix, or if somebody else spots it first please do.
 
Carlos
Generalgood article PinmemberDonsw17 Jan '09 - 17:51 
GeneralVery very very good code, but... Pinmemberseb.4924 Apr '08 - 5:20 
QuestionDoes this support multi threading calls PinmemberKanna22 Mar '08 - 5:23 
GeneralPocketPC Pinmemberrichardjjs14 Apr '07 - 21:56 
GeneralAmazing Pinmemberazam's13 Feb '07 - 18:21 
QuestionHow to implement in CF PinmemberAjatashatru13 Sep '06 - 3:19 
GeneralServiceNotification PinmemberRyan McFarren5 Jun '06 - 10:23 
QuestionMessageBoxTimeOut API function? Pinmembereshipman13 Dec '04 - 3:42 
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinsussAnonymous13 Oct '04 - 12:51 
GeneralThanks a lot Pinmemberaemmons6 Oct '04 - 9:54 
Generalpicture PinmemberTaha Zayed24 Sep '04 - 15:55 
GeneralNamespace Errors when trying to build PinmemberLordRhys3 Sep '04 - 6:49 
GeneralYou beat me to it. PinmemberAshaman13 Aug '04 - 2:54 

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

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