PacktLib: Multithreading in C# 5.0 Cookbook

Multithreading in C# 5.0 Cookbook


About the Author

About the Reviewers


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

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

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

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

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

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



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

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

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

Parallel Programming Patterns


Implementing Lazy-evaluated shared states

Implementing Parallel Pipeline with BlockingCollection

Implementing Parallel Pipeline with TPL DataFlow

Implementing Map/Reduce with PLINQ

There's More


Using a timer in a Windows Store application

Using WinRT from usual applications

Using BackgroundTask in Windows Store applications