Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

messageBox with Timeout

0.00/5 (No votes)
12 Jul 2010 1  
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

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

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

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.

//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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here