C#ThreadSleep的替代方案
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/416
作为初学者每次在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 09:15:56