【C#】45. Task ContinueWith 後續操作

來源:互聯網
上載者:User

本章內容其實挺重要的,但是現在我工作中還沒怎麼遇到,應該是我還沒想到的關係吧~

ContinueWith是Task根據其自身狀況,決定後續應該作何操作。也就是說,在運行完task後,會執行task.continuewith(XX)中的XX語句,但是是否執行、如何執行等需要看task的運行情況。

static int TaskMethod(string name, int seconds){Console.WriteLine("Task Method : Task {0} is running on a thread id {1}. Is thread pool thread: {2}",name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);Thread.Sleep(TimeSpan.FromSeconds(seconds));return 42 * seconds;}

var firstTask = new Task<int>(() => TaskMethod("第一個任務", 3));firstTask.ContinueWith(t => Console.WriteLine("第一個任務的返回結果為 {0}. Thread id {1}, 是否是線程池線程: {2}",t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread),TaskContinuationOptions.OnlyOnRanToCompletion);firstTask.Start();



Start()和ContinueWith()的先後順序沒有關係,ContinueWith()會等待直到firstTask運行狀態達到 IsCompleted,因為TaskContinuationOptions中的OnlyOnRanToCompletion.

必須指出的是,ContinueWith()中的參數是需要以Task為參數的,也就是firstTask作為參數被傳入,而且ContinueWith()運行線上程池中的線程中。

我覺得比較重要的一點是:把ContinueWith()中的語句當做一塊新的語句塊,他們獨立於主線程。無論如何,他們都要被判斷,如果狀態(status)不滿足,那麼他們不執行;當指定了多個狀態,則使用合理的對應狀態。

看下面的代碼:

Console.WriteLine("主線程 Thread id {0}, 是否是線程池線程: {1}",Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);var firstTask = new Task<int>(() => TaskMethod("第一個任務", 3));var secondTask = new Task<int>(() => TaskMethod("第二個任務", 2));firstTask.ContinueWith(t => Console.WriteLine("後續:第一個任務的返回結果為 {0}. Thread id {1}, 是否是線程池線程: {2}",t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread),TaskContinuationOptions.OnlyOnRanToCompletion);firstTask.Start();secondTask.Start();Thread.Sleep(TimeSpan.FromSeconds(4)); //給予足夠時間,讓firstTask、secondTask及其後續操作執行完畢。Task continuation = secondTask.ContinueWith(t => Console.WriteLine("後續:第二個任務的返回結果為 {0}. Thread id {1}, 是否是線程池線程: {2}",t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread),TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);

這裡主線程休眠了足足4秒鐘,足以讓firstTask和secondTask兩個任務完成運行,而後,由於secondTask的後續除了接受OnlyOnRanToCompletion外,還接受ExecuteSynchronously。因此,後續運行中,由於主線程還沒有結束,因此 ExecuteSynchronously得到認可,故secondTask的後續是在主線程上運行。


然而,如果把4秒鐘的休眠注釋掉,那麼由於主線程很早就結束了,因此secondTask只能接受到OnlyOnRanToCompletion,因此還是運行線上程池中。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.