隐藏

C#中的Task.Delay()和Thread.Sleep()

发布:2021/9/28 9:01:15作者:管理员 来源:本站 浏览次数:1061

    Thread.Sleep()是同步延迟,Task.Delay()是异步延迟。
    Thread.Sleep()会阻塞线程,Task.Delay()不会。
    Thread.Sleep()不能取消,Task.Delay()可以。
    Task.Delay()实质创建一个运行给定时间的任务,Thread.Sleep()使当前线程休眠给定时间。
    反编译Task.Delay(),基本上讲它就是个包裹在任务中的定时器。
    Task.Delay()和Thread.Sleep()最大的区别是Task.Delay()旨在异步运行,在同步代码中使用Task.Delay()是没有意义的;在异步代码中使用Thread.Sleep()是一个非常糟糕的主意。通常使用await关键字调用Task.Delay()。
    我的理解:Task.Delay(),async/await和CancellationTokenSource组合起来使用可以实现可控制的异步延迟。

参考资料:

https://www.cnblogs.com/yy1234/p/8073732.html

https://blog.csdn.net/shu19880720/article/details/72901876

https://code.msdn.microsoft.com/ThreadSleep-vs-TaskDelay-766b46b7/view/Discussions#content

https://blog.csdn.net/wushang923/article/details/41015063

http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx(评论区有争议)

https://oomake.com/question/5779232

https://walterlv.com/post/sleep-delay-zero-vs-yield.html

以下是本人调试时的代码:

代码1:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
     
    namespace Delay_And_Sleep
    {
        class Program
        {
            static void Main(string[] args)
            {
                Task.Factory.StartNew(delegate
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ****** 开始Sleep()");
                    for (int i = 1; i < 20; i++)
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ***Sleep*** " + i);
                        Thread.Sleep(100);
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ****** 结束Sleep()");
                });
     
                Task.Factory.StartNew(() =>
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") +  " ====== 开始Delay()");
                    for (int i = 101; i < 120; i++)
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ===Delay=== " + i);
                        Task.Delay(100);//需要.net4.5及以上
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ====== 结束Delay()");
                });
     
                //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "press enter to close . . .");
                Console.ReadLine();
            }
        }
    }

运行结果:

代码2:

    using System;
    using System.Threading.Tasks;
     
    namespace Delay_async_await
    {
        class Program
        {
            //该段代码通过async/awatit实现“同步”Delay
            static void Main(string[] args)
            {
                Task.Factory.StartNew(async () =>
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ====== 开始Delay()");
                    for (int i = 101; i < 120; i++)
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ===Delay=== " + i);
                        await Task.Delay(100);//需要.net4.5及以上
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ====== 结束Delay()");
                });
     
                //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "press enter to close . . .");
                Console.ReadLine();
            }
        }
    }

运行结果:

代码3:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace 取消Delay
    {
        public partial class Form1 : Form
        {
            CancellationTokenSource cts = new CancellationTokenSource();
     
            public Form1()
            {
                InitializeComponent();
            }
     
            void PutThreadSleep()
            {
                Thread.Sleep(5000);
            }
     
            async Task PutTaskDelay()
            {
                try
                {
                    await Task.Delay(5000, cts.Token);//需要.net4.5的支持
                }
                catch (TaskCanceledException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
     
            private void btnThreadSleep_Click(object sender, EventArgs e)
            {
                PutThreadSleep();
                MessageBox.Show("Sleep : I am back");
            }
     
            //使用async/await便于观察效果;不用的话就直接弹出MessageBox了
            private async void btnTaskDelay_Click(object sender, EventArgs e)
            {
                await PutTaskDelay();
                MessageBox.Show("Delay : I am back");
            }
     
            private void btnCancelTaskDelay_Click(object sender, EventArgs e)
            {
                cts.Cancel();
            }
        }
    }

运行结果: