国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Thread.Sleep vs. Task.Delay

2019-11-17 02:20:28
字體:
來源:轉載
供稿:網友

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 myprevious postexplaining 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 aTaskCanceledException.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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大兴区| 房山区| 依兰县| 盐津县| 丹棱县| 利津县| 沙湾县| 南靖县| 永嘉县| 安福县| 英德市| 甘孜| 兴义市| 白朗县| 丹东市| 尖扎县| 大新县| 乌拉特中旗| 大石桥市| 英吉沙县| 龙口市| 商水县| 扎鲁特旗| 沙洋县| 平泉县| 通州市| 通许县| 衡阳市| 慈利县| 滨州市| 漯河市| 嘉善县| 锡林浩特市| 繁峙县| 鄯善县| 南平市| 林口县| 乾安县| 健康| 邮箱| 贵州省|