Tively you can call startnew on the

Info icon This preview shows pages 685–687. Sign up to view the full content.

View Full Document Right Arrow Icon
tively, you can call StartNew on the Task<TResult>.Factory static property.) You can start a Task<TResult> and then call Wait to wait for the result, or you can read the Result property, which will call Wait for you if the result is not yet available. But blocking until the task is complete may not be especially useful—it’s just a very round- about way of invoking the code synchronously. In fact, you might sometimes want to do this—you might create multiple child tasks and then wait for all of them to complete, and you’d be able to take advantage of the TPL’s common exception-handling frame- work to manage any errors. However, it’s often useful to be able to provide some sort of callback method to be invoked once the task completes, rather than blocking. You can do this with a continuation. Continuations A continuation is a task that gets invoked when another tasks completes. The Task class provides a ContinueWith method that lets you provide the code for that continu- ation task. It requires a delegate that takes as its single argument the task that just completed. ContinueWith offers overloads that allow the delegate to return a value (in which case the continuation task will be another Task<TResult> ), or not to return a value (in which case the continuation task will just be a Task ). ContinueWith returns the Task object that represents the continuation. So you can string these things together: static void Main(string[] args) { Task t = Task.Factory.StartNew(() => Go("One", 100)) .ContinueWith(t1 => Go("Two", 500)) .ContinueWith(t2 => Go("Three", 200)); t.Wait(); } This will execute the three tasks one after another. Notice that the t variable here refers to the third task—the final continuation. So t.Wait() will wait until all the tasks are complete—it doesn’t need to wait for the first two because the final task can’t even † In case you’ve come across continuations in the sense meant by languages such as Scheme that offer call with current continuation , be aware that this is not the same idea. There’s a tenuous connection in the sense that both represent the ability to continue some work sometime later, but they’re really quite different. The Task Parallel Library | 661
Image of page 685

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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.
Image of page 686
Image of page 687
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern