且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的延迟功能

更新时间:2023-10-26 17:49:34

使用异步方法通过内置的 Task.Delay 方法创建延迟.这将导致执行被暂停,然后在指定的时间结束之后恢复执行.

Use an async method to create a delay using the built-in Task.Delay method. This will cause execution to be paused and then resumed after the specified time without blocking the current thread.

async Task UseDelay()
{
    await Task.Delay(1000); // wait for 1 second
}

根据您的具体情况

async void button1_Click(object sender, EventArgs e)
{ 
    for (var i = 0; i < 10; i++) 
    {
        textBox1.BackColor = Color.Red;         
        await Task.Delay(100);
        textBox1.BackColor = Color.Yellow;    
        await Task.Delay(100);
    }
 }