Click here to Skip to main content
13,350,708 members (70,530 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


32 bookmarked
Posted 12 Jul 2010

messageBox with Timeout

, 12 Jul 2010
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

QuestionGreat control Pin
Wrangly6-Sep-12 4:22
memberWrangly6-Sep-12 4:22 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 12 Jul 2010
Article Copyright 2010 by vicoB
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid