当前位置:首页 > 编程语言 > C# > 正文内容

C# 延时命令

C#3年前 (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;
            });

缺点:异步操作

相关文章

C# 复制图片到剪贴板

Clipboard.SetImage(pictureBox1.Image);...

C# protected

官方:只有在通过派生类类型发生访问时,基类的受保护成员在派生类中才是可访问的。 简单理解:...

C# 标准日期和时间格式说明符

Code说明Write备注Y年月2022年7月y标准日期和时间格式说明符2022年7月单独使用时y年...

DotfuscatorPro使用教程

DotfuscatorPro使用教程

1首次使用,添加反编译工具路径ILASM_v4.0.30319C:\Windows\Microsof...

C# CRC32算法

CRC32      class CRC32...

C# string与Hex互转

StrToHex    /// <summary>...