Free Sample
+ Collection

Multithreading in C# 5.0 Cookbook

Eugene Agafonov

Multithreaded programming can seem overwhelming but this book clarifies everything through its cookbook approach. Packed with practical tasks, it’s the quick and easy way to start delving deep into the power of multithreading in C#.
RRP $29.99
RRP $49.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781849697644
Paperback268 pages

About This Book

  • Delve deep into the .NET threading infrastructure and use Task Parallel Library for asynchronous programming
  • Scale out your server applications effectively
  • Master C# 5.0 asynchronous operations language support

Who This Book Is For

If you are a developer or new to multithreaded programming and you are looking for a quick and easy way to get started, then this book is for you. It is assumed that you have some experience in C# and .NET already, and you should also be familiar with computer science and basic algorithms and data structure

Table of Contents

Chapter 1: Threading Basics
Creating a thread in C#
Pausing a thread
Making a thread wait
Aborting a thread
Determining a thread state
Thread priority
Foreground and background threads
Passing parameters to a thread
Locking with a C# lock keyword
Locking with a Monitor construct
Handling exceptions
Chapter 2: Thread Synchronization
Performing basic atomic operations
Using the Mutex construct
Using the SemaphoreSlim construct
Using the AutoResetEvent construct
Using the ManualResetEventSlim construct
Using the CountDownEvent construct
Using the Barrier construct
Using the ReaderWriterLockSlim construct
Using the SpinWait construct
Chapter 3: Using a Thread Pool
Invoking a delegate on a thread pool
Posting an asynchronous operation on a thread pool
Thread pool and the degree of parallelism
Implementing a cancellation option
Using a wait handle and timeout with a thread pool
Using a timer
Using the BackgroundWorker component
Chapter 4: Using Task Parallel Library
Creating a task
Performing basic operations with a task
Combining tasks together
Converting the APM pattern to tasks
Converting the EAP pattern to tasks
Implementing a cancellation option
Handling exceptions in tasks
Running tasks in parallel
Tweaking tasks execution with TaskScheduler
Chapter 5: Using C# 5.0
Using the await operator to get asynchronous task results
Using the await operator in a lambda expression
Using the await operator with consequent asynchronous tasks
Using the await operator for the execution of parallel asynchronous tasks execution
Handling exceptions in the asynchronous operations
Avoid using the captured synchronization context
Working around the async void method
Designing a custom awaitable type
Using the dynamic type with await
Chapter 6: Using Concurrent Collections
Using ConcurrentDictionary
Implementing asynchronous processing using ConcurrentQueue
Changing asynchronous processing order ConcurrentStack
Creating a scalable crawler with ConcurrentBag
Generalizing asynchronous processing with BlockingCollection
Chapter 7: Using PLINQ
Using the Parallel class
Parallelizing a LINQ query
Tweaking the parameters of a PLINQ query
Handling exceptions in a PLINQ query
Managing data partitioning in a PLINQ query
Creating a custom aggregator for a PLINQ query
Chapter 8: Reactive Extensions
Converting a collection to asynchronous Observable
Writing custom Observable
Using Subjects
Creating an Observable object
Using LINQ queries against the observable collection
Creating asynchronous operations with Rx
Chapter 9: Using Asynchronous I/O
Working with files asynchronously
Writing an asynchronous HTTP server and client
Working with a database asynchronously
Calling a WCF service asynchronously
Chapter 10: Parallel Programming Patterns
Implementing Lazy-evaluated shared states
Implementing Parallel Pipeline with BlockingCollection
Implementing Parallel Pipeline with TPL DataFlow
Implementing Map/Reduce with PLINQ
Chapter 11: There's More
Using a timer in a Windows Store application
Using WinRT from usual applications
Using BackgroundTask in Windows Store applications

What You Will Learn

  • Work with raw threads, synchronize threads, and coordinate their work
  • Work effectively with a thread pool
  • Develop your own asynchronous API with Task Parallel Library
  • Use C# 5.0 asynchronous language features
  • Scale up your server application with I/O threads
  • Parallelize your LINQ queries with PLINQ
  • Use common concurrent collections
  • Apply different parallel programming patterns
  • Work with Windows 8 asynchronous APIs
  • Use Reactive Extensions to run asynchronous operations and manage their options

In Detail

In an age when computer processors are being developed to contain more and more cores, multithreading is a key factor for creating scalable, effective, and responsive applications. If you fail to do it correctly, it can lead to puzzling problems that take a huge amount of time to resolve. Therefore, having a solid understanding of multithreading is a must for the modern application developer.

Multithreading in C# 5.0 Cookbook is an easy-to-understand guide to the most puzzling programming problems. This book will guide you through practical examples dedicated to various aspects of multithreading in C# on Windows and will give you a good basis of practical knowledge which you can then use to program your own scalable and reliable multithreaded applications.

This book guides you through asynchronous and parallel programming from basic examples to practical, real-world solutions to complex problems. You will start from the very beginning, learning what a thread is, and then proceed to learn new concepts based on the information you get from the previous examples.

After describing the basics of threading, you will be able to grasp more advanced concepts like Task Parallel Library and C# asynchronous functions. Then, we move towards parallel programming, starting with basic data structures and gradually progressing to the more advanced patterns. The book concludes with a discussion of the specifics of Windows 8 application programming, giving you a complete understanding of how Windows 8 applications are different and how to program asynchronous applications for Windows 8.


Read More

Recommended for You

C# 2008 and 2005 Threaded Programming: Beginner's Guide
$ 23.99
C# Multithreaded and Parallel Programming
$ 29.99
Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C#
$ 13.50
GDI+ Application Custom Controls with Visual C# 2005
$ 10.50