Click here to Skip to main content
Click here to Skip to main content

Tagged as

messageBox with Timeout

, 12 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vicoB
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 Pinmembercumthyb1-Mar-14 15:51 
QuestionGreat control PinmemberWrangly6-Sep-12 3:22 
GeneralMy vote of 2 PinmemberMr.BitEX27-Sep-11 12:07 
QuestionDo you mean modal? Pinmemberarchippus21-Jul-10 5:43 
Generaldemo.cs is missing from the demo project PinmemberRozenOren19-Jul-10 20:12 
GeneralRe: demo.cs is missing from the demo project PinmemberHeinM19-Jul-10 20:19 
GeneralRe: demo.cs is missing from the demo project PinmemberMember 1062651425-Feb-14 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 12 Jul 2010
Article Copyright 2010 by vicoB
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid