C# When you open form 2 wait 5 seconds and open form 3

Asked

Viewed 2,213 times

0

I need that when loading form 2 the program waited 5 seconds in order to automatically open form 3 as I do this?

private void LetTheGameStart_Load(object sender, EventArgs e)
{
    timer1.Start();
}

this is the only code I have at the moment

  • Why not include a Timer component and when it reaches the time, open the form? Study how the Timer on the MSDN

  • Now I’ve got to figure out how to finish it after five seconds

  • 1

    Could you explain better what you are looking for? The way it is, it is not clear what you want. Click to [Edit] the question and add more information, and if possible the code you already have. This way it will be easier to find the solution you are looking for.

  • I didn’t understand one thing the alarmCounter can help me?

  • In fact you need to understand how the Timer works. The component has the Enabled properties, Interval. Every time it is active, it goes for a while until it reaches Interval. Example OS. When you hit the Interval, it triggers the event Ontick. In interval, the time is in milliseconds.

  • Take a look at the Code Project which also has good examples.

  • private void Helloform_load(Object Sender, Eventargs e) { label1.Text = "Hello " + Username; t.Interval = 5000; t.Tick += new Eventhandler(Ontimerticked); t.Start(); } public void Ontimerticked(Object Sender, Eventargs e) { t.Stop(); Letthegamestart LTGS = new Letthegamestart(); LTGS.Show(); this. Hide();

Show 2 more comments

1 answer

4


Particularly, I find it extremely unnecessary to use a timer for this type of operation. Use a timer when you want to repeat something within a certain time, or else for cases that need a little more work. You can use Task.Delay() to make your application wait certain time without locking the GUI.

private async void LetTheGameStart_Load(object sender, EventArgs e)
{
    await Task.Delay(5000);

    AbrirForm2(); // Coloque aqui o código para abrir o form2
}

With Task.Delay the desired delay is obtained. The await does not stop the application.


Using Timer

If you want to insist on Timer (this may even be better in older versions of . NET Framework) it will be necessary to define a Interval for this timer and an event Tick. The Interval is the time (in milliseconds) at which this timer will be fired and the Tick is the event that will be run. See an example:

public partial class Form1 : Form
{
    private readonly Timer timer = new Timer();

    public Form1()
    {
        InitializeComponent();
        timer.Interval = 5000; //Definir o intervalo em 5 segundos
        timer.Tick += timer_Tick; // Inscrever o evento tick
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        new Form().Show(); // Aqui deve-se abrir o Form2
        timer.Stop(); // Parar o timer, porque isso só é necessário uma vez
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer.Start(); // Iniciar o timer quando o form for carregado
    }
}
  • +1 for async/await, but the solution with Timer may prove useful if the target of .NET Framework is equal to or less than .NET 4

  • 1

    @Tobymosque Right on time =D

Browser other questions tagged

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