Show message after opening of Form

2

I am making an application where after the login window, if you are a new user, you will run a tutorial to show the use of it. The problem is that a method of the second form is executed where the messagebox is displayed before the logeo form is closed, now I have it with this code;

private void abrirInicioJuego()
    {

        fb.timerRecursos();
        FormInicial pi = new FormInicial(fb,tutorial);     
        pi.Show();
        this.Close();
    }

This code is executed in the login form by opening the initial form of the application.

    private void PaginaInicioJuego_Load(object sender, EventArgs e)
    {
       FormTimers ft = new FormTimers();
       ft.recupearUltimaConexion(id_partida);
       ft.calcularDiferenciaDeTiempos(id_partida);

        //Comienza a ejecutarse el tutorial
        if (tutorial && this.Visible)
        {
            EmpezarTuto();
        }
    }

In the load of the initial form I execute a method (StartTuto ();) But all the messages that this method contains appear before the form is even displayed, before closing the form of logeo.

    
asked by Hector Lopez 27.03.2018 в 10:37
source

1 answer

2

The event Form.Load is launched when the form starts loading. Therefore, it is possible that the tutorial appears before the form appears on the screen.

It is better to use the event Form.Shown , which is launched the first time the form is displayed on the screen.

If what you need is a delay, you have several options. On the one hand, you can create a Timer in the event Shown , and in the Tick of Timer show the tutorial and deactivate the Timer .

You can also use an anonymous method, something similar to the following:

System.Threading.Timer timer;
timer =  new System.Threading.Timer((obj) =>
         {
               EmpezarTuto();
               timer.Dispose();
         }, 
         null, 1000, System.Threading.Timeout.Infinite);

Another option is to use the class Task , with the methods Delay and ContinueWith :

Task.Delay(1000).ContinueWith(t => EmpezarTuto());

You have more information about delayed methods in this StackOverflow question

    
answered by 27.03.2018 / 10:59
source