Messagebox with autoclose

Asked

Viewed 1,626 times

1

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

  1. The app is a chat. It does not keep record of conversations;
  2. When the application is closed I request a confirmation for closure stating that all converse content will be lost;
  3. Chat stays active on Tray until the user tries to close it by left-clicking on the icon;

When the user tries to turn off the PC without before turning off the chat windows is always on the screen "Preparing to finish", waiting for the end of the pending processes and never leaves this screen without the user "Force" the termination.

I believe this behavior occurs due to this confirmation message that blocks the thread.

What I wish to do is something like:

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

But when using the MessageBox.Show() the user should always respond to terminate the process.

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

Any help that leads me to achieve my goal and make users more "Happy" will be very welcome.

  • See help: http://answall.com/questions/50873/como-fa%C3%A7o-para-fechar-encerrar-um-msgbox-via-c%C3%B3digo/50878#50878

  • 1

    I will test, but I believe it will meet my need. VLW

  • 1

    @Hstackoverflow Post your comment as response.

  • Thank you @Paulohdsousa, if you solve his problem I think it is better to evaluate the answer (from the link) otherwise it is repetitive. Even so if the author finds the answer (of the comment) useful I can change.

1 answer

1

Unfortunately, Messagebox does not implement a Timer. But you can make a dialog 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 a No. In the example I will call this form Messaging.

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 code of the Tick event, ie:

this.DialogResult = DialogResult.Yes;

And not on the button:

this.DialogResult = DialogResult.No;

Remember that when we set the Dialogresult of a form, it is automatically closed.

To call this form, put 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.