Thread.Sleep vs. Task.Delay

We use both Thread.Sleep() and Task.Delay() to suspend the execution of a program for some given time. But are we actually suspending the execution? What is the difference between these two? How to abort from a Sleeping thread or from a delaying 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 go by 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” button.

void PutThreadSleep()
{
    Thread.Sleep(5000);
}

async Task PutTaskDelay()
{
    await Task.Delay(5000, tokenSource.Token);
} 

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");
}

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

Thread.Sleep()

This is the classical way of suspending a execution. This method will suspend the current thread until the elapse of giving time.When you put the Thread.Sleep in the above way, there is nothing you can do to abort this except by waiting till the time elapses or by restarting the application. That’s because this suspends the main thread the program is running. And because of that the UI is not responsive.

Task.Delay()

When comparing to Thread.Sleep(), Task.Delay() acts in a different way. Basically Task.Delay() will create a task which will complete after a time delay. This task will be running in a different thread, UI is responsive, and that‘s because Task.Delay() is not blocking the main thread.

Behind the scene what is happening is there is a timer ticking till the specified time. Since there is a timer, anytime we can cancel the task delay 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)
   { 

   }
}

Here when the task got cancelled, it will be throwing me a TaskCanceledException. I am just catching the exception and suppressing it, because  don‘t want to show any message about that.

So in my “Cancel Task Delay” button click event, I am asking the task to be cancelled.

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

Once the task has been cancelled, the control returns immediately to the next line and message box will be shown.

https://code.msdn.microsoft.com/ThreadSleep-vs-TaskDelay-766b46b7

时间: 2024-10-17 05:13:44

Thread.Sleep vs. Task.Delay的相关文章

.Net4.0如何实现.NET4.5中的Task.Run及Task.Delay方法

前言 .NET4.0下是没有Task.Run及Task.Delay方法的,而.NET4.5已经实现,对于还在使用.NET4.0的同学来说,如何在.NET4.0下实现这两个方法呢? 在.NET4.0下,有一个泛型类,叫TaskCompletionSource<TReuslt>,它能控制Task的行为,如给Task设置结果.设置异常.设置取消等. MSDN是这样描述的(网址): 表示未绑定到委托的 Task<TResult> 的制造者方,并通过Task属性提供对使用者方的访问. 它有以

Task.Delay方法的2个应用实例,单元测试等待,限时限次下载远程资源

如果想让程序异步等待一段时间,可以考虑使用Task.Delay方法. 比如,在单元测试中模拟一个异步操作. static async Task<T> DelayedResult<T>(T result, TimeSpan delay) { await Task.Delay(delay); return result; } 又比如,当我们需要远程下载内容,由于远程服务器的状态未必稳定,如果只调用某个方法一次,未必能获取到想要的数据. 我们可以每隔一段时间调用方法一次,向远程服务器获取

【转】【C#】【Thread】【Task】多线程

多线程 多线程在4.0中被简化了很多,仅仅只需要用到System.Threading.Tasks.::.Task类,下面就来详细介绍下Task类的使用. 一.简单使用 开启一个线程,执行循环方法,返回结果.开始线程为Start(),等待线程结束为Wait(). /// <summary> /// Task简单使用 /// </summary> private void Demo1() { int i = 0; Random r = new Random(DateTime.Now.S

C#多线程总结

线程的创建 Thread 1 var thread = new Thread(() => 2 { 3 Console.WriteLine("thread start:" + Thread.CurrentThread.ManagedThreadId); //ManagedThreadId为线程的id 4 Thread.Sleep(10000); 5 Console.WriteLine("thread end:" + Thread.CurrentThread.Ma

Async/Await - Best Practices in Asynchronous Programming

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx Figure 1 Summary of Asynchronous Programming Guidelines Name Description Exceptions Avoid async void Prefer async Task methods over async void methods Event handlers Async all the way Don’t mix

Async/Await 最佳实践

其实好久以前就看过这个文章,以及类似的很多篇文章.最近在和一个新同事的交流中发现原来对async的死锁理解不是很透彻,正好最近时间比较充裕就再当一回搬运工. 本文假定你对.NET Framework 4.5 的异步编程有基本的了解,相关的建议你能够在Stack Overflow, MSDN 以及async/awai FAQ中找到.这里并不试图想讲述多少新的知识点,而是想强调几点最佳实践,以减少你阅读大量文档排查问题的时间.本文的最佳实践更多的是一些指导意见,并不是实际意义上的规则.每条意见后面都

await and async

Most people have already heard about the new “async” and “await” functionality coming in Visual Studio 11. This is Yet Another Introductory Post. First, the punchline: Async will fundamentally change the way most code is written. Yup, I believe async

Async All the Way

Asynchronous code reminds me of the story of a fellow who mentioned that the world was suspended in space and was immediately challenged by an elderly lady claiming that the world rested on the back of a giant turtle. When the man enquired what the t

Async/Await 异步编程中的最佳做法

近日来,涌现了许多关于 Microsoft .NET Framework 4.5 中新增了对 async 和 await 支持的信息. 本文旨在作为学习异步编程的“第二步”:我假设您已阅读过有关这一方面的至少一篇介绍性文章. 本文不提供任何新内容,Stack Overflow.MSDN 论坛和 async/await FAQ 这类在线资源提供了同样的建议. 本文只重点介绍一些淹没在文档海洋中的最佳做法. 本文中的最佳做法更大程度上是“指导原则”,而不是实际规则. 其中每个指导原则都有一些例外情况