locked
how do i close a message box programaticlly in c# RRS feed

  • Question

  • i want to close a messagebox in c# after a certain period of time how do i do it in c#
    Thursday, December 10, 2009 5:09 PM

Answers

All replies

  • Following is a trick
    main()
    {
    Thread t = new Thread(new ThreadStart(ThreadProc));
    t.Start();
    Thread.Sleep(yourCertainPeriod);
    if(t.IsAlive)
     t.Abort();
    }
    
    void ThreadProc()
    {
    MessageBox.Show("MessageBox");
    }
    But creating your own message box is a better solution.
    With best regards, Yasser Zamani
    • Marked as answer by eryang Friday, December 18, 2009 2:27 AM
    Thursday, December 10, 2009 5:23 PM
  • Hi,

    You have 2 options:
    1. Use MessageBox with timeout
      http://www.codeproject.com/KB/miscctrl/CsMsgBoxTimeOut.aspx or
      http://www.codeproject.com/KB/dialog/AutoCloseMessageBox.aspx
    2. Create your own MessageBox

    Regards, Danijel
    • Proposed as answer by Danijel MalikMVP Sunday, December 13, 2009 9:57 AM
    • Marked as answer by eryang Friday, December 18, 2009 2:27 AM
    Thursday, December 10, 2009 5:51 PM
  • More:

    You can try using ShowDialog Method and close the dialog whenever you want.

    Link:

    http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

    Regards,
    Jai
    • Marked as answer by eryang Friday, December 18, 2009 2:27 AM
    Sunday, December 13, 2009 6:28 AM
  • Yasser, I tested the code :

    using System;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            [STAThread]
            public static void Main(){
                Thread t = new Thread(ThreadProc);
                t.Start();
                Thread.Sleep(1000);
                if(t.IsAlive) t.Abort();
            }
    
            static void ThreadProc(){
                MessageBox.Show("MessageBox");
            }
        }
    }
    

    The message box still remains open waiting for user interaction. It doesn't close as desired. I testen on .Net Framework 2.0 & 4.5 , both with same behavior. The trick only solves the issue of avoid the main thread get-stuck waiting for user interaction, but the messageboxes launched this way (on secondary threads) still remain waiting for user interaction and despite be aborted they mantain "live" the main process. Yasser, if it's something wrong with the behavior I expose please clarify. Thank you.

    Tuesday, November 26, 2013 2:48 PM