Click here to Skip to main content
14,267,320 members

messageBox with Timeout

Rate this:
4.14 (6 votes)
Please Sign up or sign in to vote.
4.14 (6 votes)
12 Jul 2010CPOL
This article describes a simple extended message box which can be timed out


This article describes a simple extension of the standard messagebox with a timeout option. This might be very useful if timed events are used to automatically show message boxes. Using the standard messagebox in such cases will result in a lot of messageboxes on top of each other which will be very irritating for users of your application.


I use this class in my application which runs on a 24/7 base. In case of failures of the application, I have to show this to the user. If the user does not see/react to the message presented to him, my application still has to take some action. So, after an optional timeout, the messagebox is closed.

Using the Code

The code exists of one main class:

  • msgBoxExt

and two helper classes:

  • MsgBoxExtOptions
  • MsgBoxResultEventArgs

msgBoxExt Usage

 //register result event of msgboxExt		
 msgBoxExt.msgBoxResultEvent += 
	new EventHandler<MsgBoxResultEventArgs>(msgBoxExt_msgBoxResultEvent);
//timeout in ms
int timeout = 5000 //5 seconds
// is model
bool model = false;
//new MsgBoxExtOptions
MsgBoxExtOptions options = new MsgBoxExtOptions
   ("Do you want to close this application?",
   "Quit", MsgBoxResultReferences.CLOSE_ON_YES,
    MessageBoxButtons.YesNo, MessageBoxIcon.Question, timeout, model);
 //show messagebox

msgBoxExt Class

This class has one public method. The show method checks whether there is a messagebox active. If so: it does nothing if the msgBox is model and closes it if it is not active.

If timeout > 0, a timeout timer is started.

An native messagebox is started in a thread:

//main public method for showing msgBox
        public static bool Show(MsgBoxExtOptions options)
            //check whether there is a messagebox active
            if (_currentOptions != null) 
                //check whether current active Msgbox is model
                if (_currentOptions.model) 
                    return false; //do nothing
                //else close the current msgBox

            //save options in current options
            _currentOptions = options;

            //if timeout > 0  start timeout timer
            if (options.timeout > 0)
                timeoutTimer = new System.Windows.Forms.Timer();
                timeoutTimer.Tick += new EventHandler(timeoutTimer_Tick);
                timeoutTimer.Interval = options.timeout;
                timeoutTimer.Enabled = true;

            //start messagebox thread
            msgBoxThread = new Thread(new ThreadStart(startMsgBoxThread));
            msgBoxThread.IsBackground = true;
            return true; //messagebox started


On timer tick, the current messagebox is closed and a result event is generated:

private static void timeoutTimer_Tick(object sender, EventArgs e)
            // close current messagebox

            //fire result event
            onMsgBoxResultEvent(_currentOptions.resultReference, DialogResult.None);

            //dispose current options
            _currentOptions = null;

closemessagebox() makes use of killwindow() which in its turn makes use of two native functions: findwindow and sendmessage to destroy the current dialog.

//method killwindow
        private static void KillWindow(string className, string title)
            //find window handle
            IntPtr handle = FindWindow(className, title);
            //send destroy message
            SendMessage(handle, WM_NCDESTROY, (IntPtr)0, (IntPtr)0);


  • 12th July, 2010: Initial post


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


About the Author

Other Radboud University Nijmegen
Netherlands Netherlands
I completed my master in electrical engineering at the University of Twente in the Netherlands in 2007. My special interest is software/hardware application which can be used for health purposes.

Currently, I am PhD-student at the Radboud University in Nijmegen, The Netherlands. I am involved in a project in which an monitoring and feedback application is developed for daily-life usage.

My personal interests: bicycling, salsa dancing and gardening

Comments and Discussions

Questionmany thanks Pin
cumthyb1-Mar-14 15:51
membercumthyb1-Mar-14 15:51 
QuestionGreat control Pin
Wrangly6-Sep-12 3:22
memberWrangly6-Sep-12 3:22 
GeneralMy vote of 2 Pin
Mr.BitEX27-Sep-11 12:07
memberMr.BitEX27-Sep-11 12:07 
QuestionDo you mean modal? Pin
archippus21-Jul-10 5:43
memberarchippus21-Jul-10 5:43 
Generaldemo.cs is missing from the demo project Pin
Oren Rosen19-Jul-10 20:12
memberOren Rosen19-Jul-10 20:12 
GeneralRe: demo.cs is missing from the demo project Pin
HeinM19-Jul-10 20:19
memberHeinM19-Jul-10 20:19 
GeneralRe: demo.cs is missing from the demo project Pin
Member 1062651425-Feb-14 22:22
memberMember 1062651425-Feb-14 22:22 
GeneralRe: demo.cs is missing from the demo project Pin
Member 1271353812-Sep-16 9:51
memberMember 1271353812-Sep-16 9:51 
GeneralRe: demo.cs is missing from the demo project Pin
Wrangly2-Nov-16 7:10
memberWrangly2-Nov-16 7:10 

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.

Posted 12 Jul 2010

Tagged as


32 bookmarked