PacktLib: C# 2008 and 2005 Threaded Programming: Beginner's Guide

C# 2008 and 2005 Threaded Programming

Credits

About the Author

Acknowledgement

About the Reviewers

Preface

Taking Advantage of Multiprocessing and Multiple Cores

Mono-processor systems: The old gladiators

The performance waterfall

Multi-processor systems: Many warriors to win a battle

Summary

Processes and Threads

Processes—any running program

Time for action - Coding a simple CPU-intensive loop

Time for action - Changing the cores available for a process

Time for action - Changing a process priority

Time for action - Running many processes in parallel

Time for action - Testing parallelism capabilities with processes

Time for action - Using the Process Explorer

Threads—Independent parts of a process

Time for action - Listing threads with Process Explorer

Time for action - Analyzing context switches with Process Explorer

Summary

BackgroundWorker—Putting Threads to Work

RTC: Rapid thread creation

Time for action - Breaking a code in a single thread

Time for action - Defining the work to be done in a new thread

Asynchronous execution

Time for action - Understanding asynchronous execution step-by-step

Synchronous execution

Showing the progress

Time for action - Using a BackgroundWorker to report progress in the UI

Cancelling the job

Time for action - Using a BackgroundWorker to cancel the job

Time for action - Using a BackgroundWorker to detect a job completed

Time for action - Working with parameters and results

Working with multiple BackgroundWorker components

Time for action - Using many BackgroundWorker components to break the code faster

BackgroundWorker and Timer

BackgroundWorker creation on the fly

Time for action - Creating BackgroundWorker components in run-time

Summary

Thread Class—Practical Multithreading in Applications

Creating threads with the Thread class

Time for action - Defining methods for encryption and decryption

Time for action - Running the encryption in a new thread using the Thread class

Sharing data between threads

Time for action - Updating the UI while running threads

Time for action - Executing the thread synchronously

Passing parameters to threads

Time for action - Using lists for thread creation on the fly I

Time for action - Using lists for thread creation on the fly II

Summary

Simple Debugging Techniques with Multithreading

Watching multiple threads

Time for action - Understanding the difficulty in debugging concurrent threads

Time for action - Finding the threads

Time for action - Assigning names to threads

Debugging multithreaded applications as single-threaded applications

Time for action - Leaving a thread running alone

Showing partial results in multithreaded code

Time for action - Explaining the encryption procedure

Time for action - Isolating results

Summary

Understanding Thread Control with Patterns

Starting, joining, pausing, and restarting threads

Time for action - Defining methods for counting old stars

Understanding the pixels' color compositions

Time for action - Running the stars counter in many concurrent threads

Creating independent blocks of concurrent code

Time for action - Waiting for the threads' signals

Summary

Dynamically Splitting Jobs into Pieces—Avoiding Problems

Running split jobs many times

Time for action - Defining new methods for running many times

Time for action - Running a multithreaded algorithm many times

Time for action - Analyzing the memory usage

Time for action - Collecting the garbage at the right time

Generalizing the algorithms for segmentation with classes

Time for action - Creating a parallel algorithm piece class

Time for action - Using a generic method in order to create pieces

Time for action - Creating a parallel algorithm coordination class

Time for action - Adding useful classic coordination methods

Summary

Simplifying Parallelism Complexity

Specializing the algorithms for segmentation with classes

Time for action - Preparing the parallel algorithm classes for the factory method

Time for action - Creating a specialized parallel algorithm piece subclass

Time for action - Creating a specialized parallel algorithm coordination subclass

Time for action —Overriding methods in the coordination subclass

Programming the piece creation method

Time for action - Defining a new method to create an algorithm instance

Time for action - Running the Sunspot Analyzer in many concurrent independent pieces

Avoiding locks and many synchronization nightmares

Summary

Working with Parallelized Input/Output and Data Access

Queuing threads with I/O operations

Time for action - Creating a class to run an algorithm in an independent thread

Time for action - Putting the logic into methods to simplify multithreading

Time for action - Creating the methods for queuing requests

Time for action - Running concurrent encryptions on demand using a pool of threads

Parallelizing database access

Summary

Parallelizing and Concurrently Updating the User Interface

Updating the UI from independent threads

Time for action - Creating a safe method to update the user interface

Time for action - Invoking a user interface update from a thread

Time for action - Identifying threads and giving them names

Time for action - Understanding how to invoke delegates step-by-step

Time for action - Creating safe counters using delegates and avoiding concurrency problems

Reporting progress to the UI from independent threads

Time for action - Creating the classes to show a progress bar column in a DataGridView

Time for action - Creating a class to hold the information to show in the DataGridView

Time for action - Invoking multiple asynchronous user interface updates from many threads

Time for action - Updating progress percentages from worker threads

Summary

Coding with .NET Parallel Extensions

Parallelizing loops using .NET extensions

Time for action - Downloading and installing the .NET Parallel Extensions

Time for action - Downloading and installing the imaging library

Time for action - Creating an independent class to run in parallel without side effects

Time for action - Running concurrent nebula finders using a parallelized loop

Time for action - Showing the results in the UI

Time for action - Providing feedback to the UI using a producer-consumer scheme

Time for action - Invoking a UI update from a task

Parallelizing queries

Time for action - Parallelized counter

Summary

Developing a Completely Parallelized Application

Joining many different parallelized pieces into a complete application

Time for action - Creating an opacity effect in an independent thread

Time for action - Creating a safe method to change the opacity

Time for action - Creating a class to run a task in an independent thread

Time for action - Putting the logic into methods to simplify running tasks in a pool of threads

Time for action - Queuing requests, running threads, and updating the UI

Time for action - Creating a specialized parallel algorithm piece subclass to run concurrently with the pool of threads

Time for action - Creating a specialized parallel algorithm coordination subclass to run concurrently with the pool of threads

Time for action - Overriding methods in the brightness adjustment coordination subclass

Time for action - Starting new threads in a new window

Time for action - Showing new windows without blocking the user interface

Rationalizing multithreaded code

Summary