C# Task.Delay替代 Thread.Sleep()

网上有很多讨论 Thread.Sleep()替代写法的文章,这里翻阅了《C#本质论》找了如下文字:

如何提高等待精度?

测试一:利用Thread.SpinWait + Stopwatch 

Stopwatch 特点:

 Operations timed using the system's high-resolution performance counter.
  Timer frequency in ticks per second = 10000000
  Timer is accurate within 100 nanoseconds

利用Stopwatch 纳秒级的计数精度+ 线程的自旋等待,消耗CPU时间

 static void SpinWait(Stopwatch sw, int duration)
        {
            var current = sw.ElapsedMilliseconds;
            while ((sw.ElapsedMilliseconds - current) < duration)
            {
                Thread.SpinWait(10);
            }
        }

 测试代码:

 static void Main(string[] args)
        {
            Console.WriteLine("======Test ...===========");
            ThreadPool.QueueUserWorkItem((x) =>
            {
                int i = 0;
                do
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    SpinWait(sw, 500);
                    sw.Stop();
                    Console.WriteLine(sw.ElapsedMilliseconds);
                    i++;
                } while (i < 30);
            });
         
            Console.ReadLine();
}

测试结果:

从测试结果看,等待时间计算非常精确;

 写法二:参考来源他人blog

 static void Main(string[] args)
        {
            Console.WriteLine("======Test ...===========");
            Task.Delay(100).Wait();
            ThreadPool.QueueUserWorkItem((x) =>
            {
                int i = 0;
                do
                {                  
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    TimeDelay(500 * 1000);                  
                    sw.Stop();
                    Console.WriteLine(sw.ElapsedMilliseconds);
                    i++;
                } while (i < 30);
            });
 
public const int NULL = 0;
        public const int QS_TIMER = 0x10;
        public static void TimeDelay(int us)
        {
            long duetime = -10 * us;
            int hWaitTimer = CreateWaitableTimer(NULL, true, NULL);
            SetWaitableTimer(hWaitTimer, ref duetime, 0, NULL, NULL, false);
            while (MsgWaitForMultipleObjects(1, ref hWaitTimer, false, Timeout.Infinite, QS_TIMER)) ;
            CloseHandle(hWaitTimer);
        }

        [DllImport("kernel32.dll")]
        public static extern int CreateWaitableTimer(int lpTimerAttributes, bool bManualReset, int lpTimerName);


        [DllImport("kernel32.dll")]
        public static extern bool SetWaitableTimer(int hTimer, ref long pDueTime,
          int lPeriod, int pfnCompletionRoutine, // TimerCompleteDelegate
                int lpArgToCompletionRoutine, bool fResume);


        [DllImport("user32.dll")]
        public static extern bool MsgWaitForMultipleObjects(uint nCount, ref int pHandles,
          bool bWaitAll, int dwMilliseconds, uint dwWakeMask);


        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(int hObject);

测试结果: 

精度也可以,对比测试1,有1个毫秒甚至更多15毫秒数的差别; 

等待精度要求不太高的等待写法如下

 public static void Sleep(int miniSeconds)
        {
             Task.Delay(miniSeconds).Wait();
        }

或者如下面的代码:

 Task.Run(async () => {
                await Task.Delay(2000);
            });

不管什么情况都要至少执行指定时间,超时也按指定时间内结束的写法如下:

public static void RunTimeOutTask(Action action, int miniSeconds)
        {
            Task.Factory.StartNew(() => {
                action();
                Task.Delay(miniSeconds).Wait();
            }).Wait(miniSeconds);
        }

实际测试有1-15个毫秒的偏差, 可能的原因应该是Task本身的超时等待Timer也是利用了操作系统计数器的频率,Windows 的系统定时器精度是 15.625ms,必须减少时间切片的长度,才有可能实现更高的精度。

Task.Delay的代码: http://www.voidcn.com/article/p-fzumrevc-bdp.html

 参考:When to use Task.Delay, when to use Thread.Sleep?

高精度定时器实现:https://blog.gkarch.com/2015/09/high-resolution-timer.html#how-to-check-current-system-timer-resolution

 

 

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的Thread.Sleep()方法用于使线程挂起一段时间。它的作用是让当前线程暂停执行一段时间,以便给其他线程或进程使用CPU资源的机会。\[1\]当调用Thread.Sleep(1000)时,表示当前线程会被挂起1秒钟,然后再继续执行。\[1\]如果调用Thread.Sleep(0),则表示当前线程会暂时放弃CPU,让其他已经准备好运行的、具有同等优先级的线程有机会执行。\[2\]\[3\]这个方法的作用是让当前线程让位,释放一些未用的时间片给其他线程或进程使用。 #### 引用[.reference_title] - *1* [C# 理解Thread.Sleep()方法](https://blog.csdn.net/yjpfinui/article/details/121669983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C# 延迟Task.Delay()和Thread.Sleep() 学习](https://blog.csdn.net/weixin_42009898/article/details/118912633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [解决C#Thread.Sleep()的作用及用法](https://blog.csdn.net/mgtts/article/details/83740123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值