zoukankan      html  css  js  c++  java
  • C#: Thread.Sleep vs. Task.Delay (转载)

    We can use both Thread.Sleep() and Task.Delay() to suspend the execution of a program (thread) for a given timespan. But are we actually suspending the execution? What is the difference between these two? How do you abort from a sleeping thread or an awaited task? Those are some of the questions I believe most of us have. Hopefully I am trying to answer all the questions above.
     
    Let’s answer these questions using an example. I have a very simple Windows Forms application which has the following form:
    I have the following two basic helper methods and I am calling these two methods from my “Thread Sleep” and “Task Delay” buttons.
    void PutThreadSleep()
    {
        Thread.Sleep(5000);
    }
    
    async Task PutTaskDelay()
    {
        await Task.Delay(5000);
    }
    
    private void btnThreadSleep_Click(object sender, EventArgs e)
    {
        PutThreadSleep();
        MessageBox.Show("I am back");
    }
    
    private async void btnTaskDelay_Click(object sender, EventArgs e)
    {
        await PutTaskDelay();
        MessageBox.Show("I am back");
    }

    Basically there is nothing to describe about the code.  (I am not going to explain the async and await keywords here; you can find many articles in MSDN and in my previous post explaining async/await). When I click both of these buttons, the message boxes will be displayed after 5 seconds. But there are significant differences between these two implementations. Let’s find out what's going on.

    Thread.Sleep()

    This is the classic way of suspending execution. This method will suspend the current thread until the given amount of time has elapsed. When you call Thread.Sleep in the above way, there is nothing you can do to abort this except waiting until the time elapses or by restarting the application. That’s because Thread.Sleep suspends the thread that's making the call. And because I'm calling Thread.Sleep in my button event handler (running in the UI thread), the UI is not responsive while it's sleeping.

    Task.Delay()

    Task.Delay acts in a very different way than Thread.Sleep. Basically, Task.Delay will create a task which will complete after a time delay. Task.Delay is not blocking the calling thread so the UI will remain responsive.

    Behind the scenes there is a timer ticking until the specified time. Since the timer controls the delay, we can cancel the delay at any time simply by stopping the timer. To cancel, I am modifying the above PutTaskDelay method as follows:

    CancellationTokenSource tokenSource = new CancellationTokenSource();
    
    async Task PutTaskDelay()
    {
        try
        {
            await Task.Delay(5000, tokenSource.Token);
        }
        catch (TaskCanceledException ex)
        {
    
        }
        catch (Exception ex)
        {
    
        }
    }

    In the call to Task.Delay I've added a cancellation token (more on that later).  When the task gets cancelled, it will throw a TaskCanceledException. I am just catching the exception and suppressing it, because I don't want to show any message about that.

    In my “Cancel Task Delay” button click event handler, I am using the cancellation token to signal the task to be cancelled:

    private void btnCancelTaskDelay_Click(object sender, EventArgs e)
    {
        tokenSource.Cancel();
    }

    Once the task has been cancelled, the PutTaskDelay method returns immediately and the caller will show the message box.

    原文链接

  • 相关阅读:
    BDB
    nginx配置文件祥解
    区分虚方法,抽象方法如此简单
    nginx+tomcat集群负载均衡(实现session复制)
    nginx配置conf
    高手博客 网址收藏
    IE6,7,8下使用Javascript记录光标选中范围(已补全)(已解决单个节点内部重复字符的问题)
    (造福中国前端界)纯前端Js完美解决各种汉字urlencode,urldecode,编码解码问题,不借助任何字库,内码表,轻松一行,兼容IE8+,chrome,firefox,safari等主流浏览器
    共享两个最近手机项目的HTML5的widget,星星评级以及标签选项卡,样式美观,iphone/android可用
    共享下改进后的combox,页面原型参考dojo的,比网上jQuery的那些combox功能强,代码更小
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/12182886.html
Copyright © 2011-2022 走看看