EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

C#ThreadSleep的替代方案

作为初学者每次在C#程序的时候,需要等待一个时间,总会使用Thread.Sleep()函数,但是一旦情况复杂起来,你会发现等待过程中会操作界面的卡死。

要么开辟线程,不过没有那个必要,比起比较繁琐的线程等操作,可以直接用下面的代码代替:

public static void Delay(int mm)
{
    DateTime current = DateTime.Now;
    while (current.AddMilliseconds(mm) > DateTime.Now)
    {
        Application.DoEvents();
    }
    return;
}

使用例:

Delay(500);

参考来源:

https://blog.csdn.net/CatchMe_439/article/details/88548680

This article was last edited at 2020-08-15 18:15:56

* *