Tham khảo tài liệu 'programming c# phần 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | start until the others are finished waiting for the last task implicitly means waiting for all three here. Continuations are slightly more interesting when the initial task produces a result the continuation can then do something with the output. For example you might have a task that fetches some data from a server and then have a continuation that puts the result into the user interface. Of course we need to be on the correct thread to update the UI but the TPL can help us with this. Schedulers The TaskScheduler class is responsible for working out when and how to execute tasks. If you don t specify a scheduler you ll end up with the default one which uses the thread pool. But you can provide other schedulers when creating tasks both StartNew and ContinueWith offer overloads that accept a scheduler. The TPL offers a scheduler that uses the Synchronizationcontext which can run tasks on the UI thread. Example 16-21 shows how to use this in an event handler in a WPF application. Example 16-21. Continuation on a UI thread void OnButtonClick object sender RoutedEventArgs e TaskScheduler uiScheduler Task string . GetData .ContinueWith task UpdateUi uiScheduler string GetData WebClient w new WebClient return http void UpdateUi string info info This example creates a task that returns a string using the default scheduler. This task will invoke the GetData function on a thread pool thread. But it also sets up a continuation using a TaskScheduler that was obtained by calling FromCurrentSynchronization Context. This grabs the SynchronizationContext class s Current property and returns a scheduler that uses that context to run all tasks. Since the continuation specifies that it wants to use this scheduler it will run the UpdateUi method on the UI thread. The upshot is that GetData runs on a thread pool thread and then its return value is passed into .