简体   繁体   English

如何在C#中的代码行之间设置延迟?

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

I am new to c# and I am using windows forms. 我是C#的新手,正在使用Windows窗体。

I have form1 with 3 buttons . 我有3个buttons form1 I am trying to set the 3 buttons so they appear one by one when/after the form loads . 我正在尝试设置3个按钮,以便它们在表单加载时/之后一一显示。 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. 谁能帮我在加载表单时/之后如何使3个按钮一一出现。 I am happy to receive any other ideas . 我很高兴收到任何其他想法。 Thank you 谢谢

You are blocking the UI thread with Thread.Sleep, and the updates aren't being reflected. 您正在使用Thread.Sleep阻止UI线程,并且更新未得到反映。

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: 最好的办法是创建一个异步函数并使用Task.Delay,这样线程将仍然响应,您将看到更新:

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;


}

The main issue here is that Load is called before the form is rendered. 这里的主要问题是在呈现表单之前调用Load。 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. 这与Thread.Sleep阻止当前(UI)线程的事实相结合,导致按钮在表单实际呈现之前全部可见。

You can solve this by using the Form.Shown event and making your pauses asynchronous. 您可以通过使用Form.Shown事件并使暂停异步来解决此问题。

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; 确保按钮仍然以Visible = false;初始化Visible = false;

Set the Visible of all buttons to False in Form_Load event as you do. 像执行操作一样,在Form_Load事件中将所有按钮的可见设置为False。
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: 在Timer.Tick事件中,根据计数器值,一一设置按钮的可见性,如下所示:

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. 然后,当所有按钮都可见时,禁用计时器。

            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; });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM