当前位置:首页 > 开发 > 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://pythonopen.com/?id=162

相关文章

C# Browsable(bool)

在编程中(比如常见的 C# 语言在开发 Windows Forms 等应用程序时),Browsabl...

C# 类接口

定义接口是一种抽象类型,它定义了一组方法签名(方法名称、参数列表和返回类型),但没有方法体。接口用于...

c# Invalidate、Refresh、Refreshitem、Refreshitems的功能

Invalidate方法功能概述Invalidate方法主要用于使控件的特定区域(整个控件或部分区域...

C# i++和++i的区别

核心区别操作顺序            ...