Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#

messageBox with Timeout

Rate me:
Please Sign up or sign in to vote.
4.14/5 (6 votes)
12 Jul 2010CPOL1 min read 94.3K   5.3K   32   11
This article describes a simple extended message box which can be timed out

Introduction

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.

Background

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

C#
 //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.Show(options);

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:

C#
//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
                closeMessageBox(_currentOptions.caption);
            }

            //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;
            msgBoxThread.Start();
            
            return true; //messagebox started

        } 

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

C#
private static void timeoutTimer_Tick(object sender, EventArgs e)
        {
            // close current messagebox
            closeMessageBox(_currentOptions.caption);

            //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.

C#
//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);
        }

History

  • 12th July, 2010: Initial post

License

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


Written By
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

 
Questionmissing demo.cs Pin
Léon de Vos8-Aug-22 21:51
Léon de Vos8-Aug-22 21:51 
Questiondemo.* is missing Pin
Member 139574126-Mar-20 18:06
Member 139574126-Mar-20 18:06 
Questionmany thanks Pin
cumthyb1-Mar-14 15:51
cumthyb1-Mar-14 15:51 
QuestionGreat control Pin
Wrangly6-Sep-12 3:22
Wrangly6-Sep-12 3:22 
GeneralMy vote of 2 Pin
Mr.BitEX27-Sep-11 12:07
Mr.BitEX27-Sep-11 12:07 
QuestionDo you mean modal? Pin
archippus21-Jul-10 5:43
archippus21-Jul-10 5:43 
Generaldemo.cs is missing from the demo project Pin
Oren Rosen19-Jul-10 20:12
Oren Rosen19-Jul-10 20:12 
GeneralRe: demo.cs is missing from the demo project Pin
HeinM19-Jul-10 20:19
HeinM19-Jul-10 20:19 
GeneralRe: demo.cs is missing from the demo project Pin
Member 1062651425-Feb-14 22:22
Member 1062651425-Feb-14 22:22 
GeneralRe: demo.cs is missing from the demo project Pin
Member 1271353812-Sep-16 9:51
Member 1271353812-Sep-16 9:51 
GeneralRe: demo.cs is missing from the demo project Pin
Wrangly2-Nov-16 7:10
Wrangly2-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.