Kate
Kate

Reputation: 945

How can I set delay between code lines in C#?

I am new to c# and I am using windows forms.

I have form1 with 3 buttons. I am trying to set the 3 buttons so they appear one by one when/after the form loads . I used the following code but it did not work when I run the program the 3 buttons appear in the same time

 private void MainForm_Load(object sender, EventArgs e)
    {    //Hide all buttons
        button1.Visible = false;
        button2.Visible = false;
        button3.Visible = false;


        Thread.Sleep(500);

       //show buttons one by one
        button1.Visible = true;
        Thread.Sleep(500);
        button2.Visible = true;
        Thread.Sleep(500);
        button3.Visible = true;


    }

I do not know what I am doing wrong. Can anyone help me how can I make the 3 buttons appear one by one when/after the form loads. I am happy to receive any other ideas . Thank you

Upvotes: 1

Views: 2603

Answers (4)

AnjumSKhan
AnjumSKhan

Reputation: 9827

            button1.Visible = false;
            button2.Visible = false;

            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(2000);
                this.Invoke((MethodInvoker)delegate { button1.Visible = true; });

                Thread.Sleep(2000);
                this.Invoke((MethodInvoker)delegate { button2.Visible = true; });
            });

Upvotes: 0

sara
sara

Reputation: 3589

The main issue here is that Load is called before the form is rendered. This, combined with the fact that Thread.Sleep blocks the current (UI) thread causes the buttons to all be made visible before the form actually renders.

You can solve this by using the Form.Shown event and making your pauses asynchronous.

private async void Shown(Object sender, EventArgs e) {
    var buttons = new[] {button1, button2, button3};
    foreach (var button in buttons)
    {
        await Task.Delay(TimeSpan.FromMilliseconds(500));
        button.Visible = true;
    }
}

Make sure that your buttons still get initiated with Visible = false;

Upvotes: 4

SashaDu
SashaDu

Reputation: 376

Set the Visible of all buttons to False in Form_Load event as you do.
Create a timer with the desired interval and some counter.
Inside the Timer.Tick event, set visibility of your buttons one by one, depending on the counter value, like this:

switch(counter)
{
  case 0:  button1.Visible = true; break;
  case 1:  button2.Visible = true; break;
  case 2:  button3.Visible = true; break;
}
counter++;

Then when all your buttons are visible, disable the timer.

Upvotes: 1

Gusman
Gusman

Reputation: 15151

You are blocking the UI thread with Thread.Sleep, and the updates aren't being reflected.

The best you can do is to create an async function and use Task.Delay, in this way the thread will be still responsive and you will see the updates:

private async void MainForm_Load(object sender, EventArgs e)
{    //Hide all buttons
    button1.Visible = false;
    button2.Visible = false;
    button3.Visible = false;


    await Task.Delay(500);

   //show buttons one by one
    button1.Visible = true;
    await Task.Delay(500);
    button2.Visible = true;
    await Task.Delay(500);
    button3.Visible = true;


}

Upvotes: 6

Related Questions