Book Image

.NET 4.5 Parallel Extensions Cookbook

By : Bryan Freeman
Book Image

.NET 4.5 Parallel Extensions Cookbook

By: Bryan Freeman

Overview of this book

<p>.NET parallel extensions brings the power of parallel and asynchronous programming to a much wider developer audience than ever before. This book will give a developer with no multithreaded development experience the ability to write highly scalable parallel applications that take advantage of modern multicore processors.If you are an experienced .NET developer who wants to put parallel extensions to work in your applications, this book is for you.</p> <p>".NET 4.5 Parallel Extensions Cookbook" is a practical, hands-on guide that provides you with a number of clear step-by-step recipes that will introduce parallelism into your applications and take advantage of modern multicore processors. This book is a crash course in using the extensions, with theory and concepts kept to a minimum.</p> <p>".NET 4.5 Parallel Extensions Cookbook" offers a wide-ranging presentation of parallel development concepts, and provides a working knowledge of key technologies that are important to developers who want to take advantage of multi-core architectures.</p> <p>You will learn how to compose a series of producer/consumer tasks into a pipeline that can process data elements received from a real-time event stream. You will also learn how to connect the stages of pipelines together using the concurrent collections. You will learn everything you need to know to transform the multicore power found in modern processors into application performance and scalability.</p>
Table of Contents (16 chapters)
.NET 4.5 Parallel Extensions Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Cancelling async operation after timeout period


Another common scenario for cancelling asynchronous tasks is to set a timeout period by using the CancellationTokenSource.CancelAfter method. This method schedules the cancellation of any associated tasks that aren't complete within the period of time that's designated by the CancelAfter expression.

In this recipe, we are going to create a WPF application that gets the contents of a classic book over the web and performs a word count. This time though, we are going to set a timeout period after which the task gets cancelled.

How to do it…

Now, let's see how we can create an asynchronous task that cancels after a specified timeout period.

  1. Start a new project using the WPF Application project template and assign CancelAfterTimeout as Solution name.

  2. Begin by opening MainWindow.xaml and add the following XAML to create our user interface:

    <Window x:Class="CancelAfterTimeout.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation...