当前位置:首页 > 开发 > C# > 正文内容

C# 延时命令

C#4年前 (2021-08-26)

方法1

System.Threading.Thread.Sleep(1000);

缺点:如果在主线程使用,命令执行完成之前,程序会进入假死状态,

方法2

        public static void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)
            {
                Application.DoEvents();
            }
        }

缺点:不会假死,但占用一定量的CPU,

方法3

        public static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }

缺点:不会假死,但占用一定量的CPU,

方式4

            var task_1 = Task.Run(async delegate
            {
                await Task.Delay(3000);
                Console.WriteLine("3秒后执行,方式一 输出语句...");
                return;
            });

缺点:异步操作

转载请注明出处。

本文链接:http://www.pythonopen.com/?id=162

相关文章

C# 获取Windows系统目录

Environment.GetFolderPath(Environment.SpecialFolde...

C# 获取MD5

命名空间     using System.Securi...

C# XML

创建    XmlDocument xmlDoc ...

在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式

要在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式,你需要考虑以下几点:1. 不...

C# 可空参数

using System; using System.Runtime.Inte...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...