MessageBox with autoclose

1

I'm having some problems with an application I created, using C# and Windows Forms , and the situation is as follows:

  • The application is a chat. It does not keep track of conversations;
  • When the application is closed I request a close confirmation that all content in the conversation will be lost;
  • The chat is active in the tray until the user tries to close it by left clicking on the icon;
  • When the user tries to shut down the PC without first turning off the chat, windows will always be in the "Preparing to finish" screen, waiting for the pending processes to close and never leave this screen without the user "Force" / p>

    I believe this behavior occurs because of this confirmation message that blocks thread .

    What I want to do is something like:

    Request confirmation, but if the user does not respond within 30 seconds, for example, the confirmation is closed automatically and the application terminates.

    However when using MessageBox.Show() the user must always respond to close the process.

    I thought of developing a form to make this notification, but I do not know how to simulate the behavior of MessageBox.Show() by blocking the thread of the UI and still check timer to see if time has passed.

    Any help that will lead me to achieve my goal and leave the users more "Happy" will be very welcome.

        
    asked by anonymous 23.03.2015 / 15:36

    1 answer

    1

    Unfortunately, the MessageBox does not implement a Timer. But you can make a dialogue form that meets your needs. For this you can create a form in design mode with a label to show the message and two buttons, a Yes and another No. In the example I will call this form of Confirmation Message.

    Replace the constructor of this new form with this:

    public MensagemConfirmacao(string msg){
        this.lblMensagem = msg;
        Timer timer = new Timer(); // cria um temporizador para fechar o form
        timer.interval = 30000; // 30000 milisegundos para executar
        timer.Tick += delegate { // atribui o evento Tick ao timer
            this.DialogResult = DialogResult.Yes; // Atribui DialogResult ao form. Isso 
                                                  // fecha o formulário
        }
        timer.Start(); // inicia o timer
    }
    

    With this, you close the form after the time set by the Timer.

    On the SIM button you put the same Event Tick code, ie:

    this.DialogResult = DialogResult.Yes;
    

    And the no button:

    this.DialogResult = DialogResult.No;
    

    Remembering that when we set the DialogResult of a form, it automatically closes.

    To call this form, place it in the OnClosing event of the main Form:

    using (MensagemConfirmacao msg = new MensagemConfirmacao("COLOQUE AQUI O TEXTO A SER EXIBIDO"))
    {
         if (msg.ShowDialog() == DialogResult.No)
            e.cancel = true; // nesse caso, "e" é o nome do parametro EventArgs do Metodo     
                             // Form_Closing
    }
    

    Any questions just ask.

        
    23.03.2015 / 17:55