Click here to Skip to main content
6,633,937 members and growing! (21,285 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

MessageBox with a timeout for .NET

By RodgerB

Just like MessageBox.Show but with an added timeout parameter
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:12 Aug 2004
Views:94,157
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 6.78 Rating: 4.69 out of 5

1

2
2 votes, 7.1%
3
7 votes, 25.0%
4
19 votes, 67.9%
5

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


Member

Occupation: Software Developer (Senior)
Company: Bloomberg
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Generalgood article PinmemberDonsw18:51 17 Jan '09  
GeneralVery very very good code, but... Pinmemberseb.496:20 24 Apr '08  
QuestionDoes this support multi threading calls PinmemberKanna6:23 22 Mar '08  
GeneralRe: Does this support multi threading calls PinmemberRodgerB14:00 31 Mar '08  
GeneralPocketPC Pinmemberrichardjjs22:56 14 Apr '07  
GeneralRe: PocketPC PinmemberRodgerB5:50 15 Apr '07  
GeneralAmazing Pinmemberazam's19:21 13 Feb '07  
GeneralRe: Amazing PinmemberAndreas Hollmann1:24 2 May '07  
QuestionHow to implement in CF PinmemberAjatashatru4:19 13 Sep '06  
AnswerRe: How to implement in CF PinmemberRodgerB4:46 13 Sep '06  
GeneralServiceNotification PinmemberRyan McFarren11:23 5 Jun '06  
GeneralMessageBoxTimeOut API function? Pinmembereshipman4:42 13 Dec '04  
GeneralRe: MessageBoxTimeOut API function? PinmemberRodgerB6:08 13 Dec '04  
GeneralAppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinsussAnonymous13:51 13 Oct '04  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberRodgerB17:17 13 Oct '04  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinsussAnonymous18:22 13 Oct '04  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberTim McCurdy2:56 7 Dec '04  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberDan Ganiere6:13 30 Aug '05  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberJ Whattam20:36 4 May '06  
GeneralRe: AppDomain.GetCurrentThreadId() is deprecated with Visual Studio 2005 Beta 1 PinmemberJanis Ozo0:21 11 Feb '07  
GeneralThanks a lot Pinmemberaemmons10:54 6 Oct '04  
Generalpicture PinmemberTaha Zayed16:55 24 Sep '04  
GeneralRe: picture PinmemberRodgerB0:51 25 Sep '04  
GeneralNamespace Errors when trying to build PinmemberLordRhys7:49 3 Sep '04  
GeneralRe: Namespace Errors when trying to build PinmemberRodgerB8:00 3 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Aug 2004
Editor: Nishant Sivakumar
Copyright 2004 by RodgerB
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project