You are previewing C# 2008 and 2005 Threaded Programming: Beginner's Guide.
O'Reilly logo
C# 2008 and 2005 Threaded Programming: Beginner's Guide

Book Description

Exploit the power of multiple processors for faster, more responsive software.

  • Develop applications that run several tasks simultaneously to achieve greater performance, scalability, and responsiveness in your applications

  • Build and run well-designed and scalable applications with C# parallel programming.

  • In-depth practical approach to help you become better and faster at managing different processes and threads

  • Optimized techniques on parallellized processes for advanced concepts

  • Packed with hands-on-examples and clear explanations for better understanding

In Detail

Most modern machines have dual core processors. This means that multitasking is built right into your computers hardware. Using both cores means your applications can process data faster and be more responsive to users. But to fully exploit this in your applications, you need to write multithreading code, which means learning some challenging new concepts.

This book will guide you through everything you need to start writing multithreaded C# applications. You will see how to use processes and threads in C#, .NET Framework features for concurrent programming, sharing memory space between threads, and much more. The book is full of practical, interesting examples and working code.

This book begins with the fundamental concepts such as processes, threads, mono-processor systems, multi-processor systems. As the book progresses, the readers get a clear understanding of starting, joining, pausing and restarting threads. The readers get a better understanding of the simple techniques associated with parallelism. There are short exercises at the end of every chapter for the readers to perform.

The book also includes several practical parallelism algorithms and data structures used for illustration, and best practices and practical topics like debugging and performance.

A practical guide to developing responsive multi-threaded and multi-process applications in C#

Table of Contents

  1. C# 2008 and 2005 Threaded Programming
    1. C# 2008 and 2005 Threaded Programming
    2. Credits
    3. About the Author
    4. Acknowledgement
    5. About the Reviewers
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who is this book for
      4. Conventions
      5. Time for action - Uploading a document
        1. What just happened?
        2. Pop quiz
        3. Have a go hero
      6. Reader feedback
      7. Customer support
        1. Downloading the example code for the book
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Taking Advantage of Multiprocessing and Multiple Cores
      1. Mono-processor systems: The old gladiators
        1. Single core: Only one warrior to fight against everybody
        2. Doing a tiny bit of each task
      2. The performance waterfall
        1. Have a go hero - Researching micro-architectures and applications
      3. Multi-processor systems: Many warriors to win a battle
        1. Have a go hero - Multi-processing systems
        2. Estimating performance improvements
        3. Have a go hero - Calculating an estimated performance improvement
        4. Avoiding bottlenecks
        5. Have a go hero - Detecting bottlenecks
        6. Taking advantage of multiple execution cores
        7. Have a go hero - Counting cores
        8. Scalability
        9. Have a go hero - Detecting scalability problems
        10. Load balancing: Keeping everybody happy
        11. Have a go hero - Thinking about load balancing
        12. Operating systems and virtual machines
        13. Parallelism is here to stay
        14. Have a go hero - Preparing minds for parallelism
      4. Summary
    8. 2. Processes and Threads
      1. Processes—any running program
      2. Time for action - Coding a simple CPU-intensive loop
        1. What just happened?
      3. Time for action - Changing the cores available for a process
        1. What just happened?
        2. Relating processes to cores
      4. Time for action - Changing a process priority
        1. What just happened?
        2. Linear code problems in multiprocessing systems
      5. Time for action - Running many processes in parallel
        1. What just happened?
      6. Time for action - Testing parallelism capabilities with processes
        1. What just happened?
      7. Time for action - Using the Process Explorer
      8. Threads—Independent parts of a process
      9. Time for action - Listing threads with Process Explorer
        1. Have a go hero - Searching multithreaded applications
      10. Time for action - Analyzing context switches with Process Explorer
        1. What just happened?
        2. Multiple threads in servers
        3. Multiple threads in clients
        4. Have a go hero - Redesigning algorithms using pseudo-code
      11. Summary
    9. 3. BackgroundWorker—Putting Threads to Work
      1. RTC: Rapid thread creation
      2. Time for action - Breaking a code in a single thread
        1. What just happened?
      3. Time for action - Defining the work to be done in a new thread
        1. What just happened?
        2. Have a go hero - Adding UI elements and monitoring the application
      4. Asynchronous execution
      5. Time for action - Understanding asynchronous execution step-by-step
        1. What just happened?
      6. Synchronous execution
      7. Showing the progress
      8. Time for action - Using a BackgroundWorker to report progress in the UI
        1. What just happened?
        2. Have a go hero - Reporting progress in many ways
      9. Cancelling the job
      10. Time for action - Using a BackgroundWorker to cancel the job
        1. What just happened?
      11. Time for action - Using a BackgroundWorker to detect a job completed
        1. What just happened?
      12. Time for action - Working with parameters and results
        1. What just happened?
        2. Have a go hero - Enhancing the application
      13. Working with multiple BackgroundWorker components
      14. Time for action - Using many BackgroundWorker components to break the code faster
        1. What just happened?
        2. Have a go hero - Monitoring and enhancing the application
      15. BackgroundWorker and Timer
      16. BackgroundWorker creation on the fly
      17. Time for action - Creating BackgroundWorker components in run-time
        1. What just happened?
        2. Have a go hero - Enhancing the code
        3. Pop quiz
      18. Summary
    10. 4. Thread Class—Practical Multithreading in Applications
      1. Creating threads with the Thread class
      2. Time for action - Defining methods for encryption and decryption
        1. What just happened?
      3. Time for action - Running the encryption in a new thread using the Thread class
        1. What just happened?
        2. Decoupling the UI
        3. Creating a new thread
        4. Retrieving data from threads
      4. Sharing data between threads
      5. Time for action - Updating the UI while running threads
        1. What just happened?
        2. Sharing some specific data between threads
        3. A BackgroundWorker helping a Thread class
      6. Time for action - Executing the thread synchronously
        1. What just happened?
        2. Main and secondary threads
        3. Have a go hero - Concurrent encryption algorithms
      7. Passing parameters to threads
      8. Time for action - Using lists for thread creation on the fly I
        1. What just happened?
      9. Time for action - Using lists for thread creation on the fly II
        1. What just happened?
        2. Creating as many threads as the number of cores
        3. Receiving parameters in the thread method
        4. Have a go hero - Concurrent UI feedback
        5. Pop quiz
      10. Summary
    11. 5. Simple Debugging Techniques with Multithreading
      1. Watching multiple threads
      2. Time for action - Understanding the difficulty in debugging concurrent threads
        1. What just happened?
        2. Debugging concurrent threads
      3. Time for action - Finding the threads
        1. What just happened?
        2. Understanding the information shown in the Threads window
      4. Time for action - Assigning names to threads
        1. What just happened?
        2. Identifying the current thread at runtime
      5. Debugging multithreaded applications as single-threaded applications
      6. Time for action - Leaving a thread running alone
        1. What just happened?
        2. Freezing and thawing threads
        3. Viewing the call stack for each running thread
        4. Have a go hero - Debugging and enhancing the encryption algorithm
      7. Showing partial results in multithreaded code
      8. Time for action - Explaining the encryption procedure
        1. What just happened?
        2. Showing thread-safe output
      9. Time for action - Isolating results
        1. What just happened?
        2. Understanding thread information in tracepoints
        3. Have a go hero - Concurrent decryption
        4. Pop quiz
      10. Summary
    12. 6. Understanding Thread Control with Patterns
      1. Starting, joining, pausing, and restarting threads
      2. Time for action - Defining methods for counting old stars
        1. What just happened?
        2. Avoiding conflicts
        3. Splitting image processing
      3. Understanding the pixels' color compositions
      4. Time for action - Running the stars counter in many concurrent threads
        1. What just happened?
      5. Creating independent blocks of concurrent code
        1. Using flags to enhance control over concurrent threads
        2. Rebuilding results to show in the UI
        3. Testing results with Performance Monitor and Process Explorer
      6. Time for action - Waiting for the threads' signals
        1. What just happened?
        2. Using the AutoResetEvent class to handle signals between threads
        3. Using the WaitHandle class to check for signals
        4. Have a go hero - Pausing and restarting threads with flags
        5. Pop quiz
      7. Summary
    13. 7. Dynamically Splitting Jobs into Pieces—Avoiding Problems
      1. Running split jobs many times
      2. Time for action - Defining new methods for running many times
        1. What just happened?
      3. Time for action - Running a multithreaded algorithm many times
        1. What just happened?
        2. Using classes, methods, procedures, and functions with multithreading capabilities
      4. Time for action - Analyzing the memory usage
        1. What just happened?
        2. Understanding the garbage collector with multithreading
      5. Time for action - Collecting the garbage at the right time
        1. What just happened?
        2. Controlling the system garbage collector with the GC class
        3. Avoiding garbage collection problems
        4. Avoiding inefficient processing usage problems
        5. Have a go hero - Queuing threads and showing progress
        6. Retrieving the total memory thought to be allocated
      6. Generalizing the algorithms for segmentation with classes
      7. Time for action - Creating a parallel algorithm piece class
        1. What just happened?
      8. Time for action - Using a generic method in order to create pieces
        1. What just happened?
        2. Creating the pieces
      9. Time for action - Creating a parallel algorithm coordination class
        1. What just happened?
        2. Starting the threads associated to the pieces
        3. Accessing instances and variables from threads' methods
      10. Time for action - Adding useful classic coordination methods
        1. What just happened?
        2. Have a go hero - Splitting algorithms specializing classes
        3. Pop quiz
      11. Summary
    14. 8. Simplifying Parallelism Complexity
      1. Specializing the algorithms for segmentation with classes
      2. Time for action - Preparing the parallel algorithm classes for the factory method
        1. What just happened?
        2. Defining the class to instantiate
        3. Preparing the classes for inheritance
      3. Time for action - Creating a specialized parallel algorithm piece subclass
        1. What just happened?
        2. Creating a complete piece of work
        3. Writing the code for a thread in an instance method
      4. Time for action - Creating a specialized parallel algorithm coordination subclass
        1. What just happened?
        2. Creating simple constructors
      5. Time for action —Overriding methods in the coordination subclass
        1. What just happened?
      6. Programming the piece creation method
        1. Programming the results collection method
      7. Time for action - Defining a new method to create an algorithm instance
        1. What just happened?
        2. Forgetting about threads
      8. Time for action - Running the Sunspot Analyzer in many concurrent independent pieces
        1. What just happened?
        2. Optimizing and encapsulating parallel algorithms
        3. Achieving thread affinity
      9. Avoiding locks and many synchronization nightmares
        1. Have a go hero - Avoiding side-effects
        2. Pop quiz
      10. Summary
    15. 9. Working with Parallelized Input/Output and Data Access
      1. Queuing threads with I/O operations
      2. Time for action - Creating a class to run an algorithm in an independent thread
        1. What just happened?
      3. Time for action - Putting the logic into methods to simplify multithreading
        1. What just happened?
        2. Avoiding Input/Output bottlenecks
        3. Using concurrent streams
        4. Controlling exceptions in threads
      4. Time for action - Creating the methods for queuing requests
        1. What just happened?
        2. Using a pool of threads with the ThreadPool class
        3. Managing the thread queue in the pool
      5. Time for action - Running concurrent encryptions on demand using a pool of threads
        1. What just happened?
        2. Converting single-threaded tasks to a multithreaded pool
        3. Encapsulating scalability
        4. Thread affinity in a pool of threads
        5. Have a go hero - Managing the pool of threads
      6. Parallelizing database access
        1. Have a go hero - Creating a parallelized data access algorithm
        2. Pop quiz
      7. Summary
    16. 10. Parallelizing and Concurrently Updating the User Interface
      1. Updating the UI from independent threads
      2. Time for action - Creating a safe method to update the user interface
        1. What just happened?
        2. Creating delegates to make cross-thread calls
        3. Figuring out the right thread to make the call to the UI
        4. Avoiding UI update problems with a delegate
        5. Retrieving results from a synchronous delegate invoke
      3. Time for action - Invoking a user interface update from a thread
        1. What just happened?
        2. Providing feedback when the work is finished
      4. Time for action - Identifying threads and giving them names
        1. What just happened?
      5. Time for action - Understanding how to invoke delegates step-by-step
        1. What just happened?
        2. Decoding the delegates and concurrency puzzle
      6. Time for action - Creating safe counters using delegates and avoiding concurrency problems
        1. What just happened?
        2. Taking advantage of the single-threaded UI to create safe counters
        3. Have a go hero - Implementing a Model-View-Controller design
      7. Reporting progress to the UI from independent threads
      8. Time for action - Creating the classes to show a progress bar column in a DataGridView
        1. What just happened?
      9. Time for action - Creating a class to hold the information to show in the DataGridView
        1. What just happened?
      10. Time for action - Invoking multiple asynchronous user interface updates from many threads
        1. What just happened?
        2. Creating a delegate without parameters
        3. Invoking a delegate asynchronously to avoid performance degradation
      11. Time for action - Updating progress percentages from worker threads
        1. What just happened?
        2. Providing feedback while the work is being done
        3. Have a go hero - Creating a parallelized user interface
        4. Pop quiz
      12. Summary
    17. 11. Coding with .NET Parallel Extensions
      1. Parallelizing loops using .NET extensions
      2. Time for action - Downloading and installing the .NET Parallel Extensions
        1. What just happened?
        2. No silver bullet
      3. Time for action - Downloading and installing the imaging library
        1. What just happened?
      4. Time for action - Creating an independent class to run in parallel without side effects
        1. What just happened?
        2. Counting and showing blobs while avoiding side effects
      5. Time for action - Running concurrent nebula finders using a parallelized loop
        1. What just happened?
        2. Using a parallelized ForEach loop
        3. Coding with delegates in parallelized loops
        4. Working with a concurrent queue
        5. Controlling exceptions in parallelized loops
      6. Time for action - Showing the results in the UI
        1. What just happened?
        2. Combining delegates with a BackgroundWorker
        3. Retrieving elements from a concurrent queue in a producer-consumer scheme
      7. Time for action - Providing feedback to the UI using a producer-consumer scheme
        1. What just happened?
        2. Creating an asynchronous task combined with a synchronous parallel loop
        3. Changing the threads' names while debugging
      8. Time for action - Invoking a UI update from a task
        1. What just happened?
        2. Providing feedback when each job is finished
        3. Using lambda expressions to simplify the code
        4. Parallelizing loops with ranges
      9. Parallelizing queries
      10. Time for action - Parallelized counter
        1. What just happened?
        2. Parallelizing LINQ queries with PLINQ
        3. Specifying the degree of parallelism for PLINQ
        4. Parallelizing statistics and multiple queries
        5. Have a go hero - Creating a parallelized user interface
        6. Pop quiz
      11. Summary
    18. 12. Developing a Completely Parallelized Application
      1. Joining many different parallelized pieces into a complete application
      2. Time for action - Creating an opacity effect in an independent thread
        1. What just happened?
        2. Running code out of the UI thread
      3. Time for action - Creating a safe method to change the opacity
        1. What just happened?
        2. Blocking the UI—Forbidden with multithreading code
      4. Time for action - Creating a class to run a task in an independent thread
        1. What just happened?
      5. Time for action - Putting the logic into methods to simplify running tasks in a pool of threads
        1. What just happened?
      6. Time for action - Queuing requests, running threads, and updating the UI
        1. What just happened?
        2. Combining threads with a pool of threads and the UI thread
      7. Time for action - Creating a specialized parallel algorithm piece subclass to run concurrently with the pool of threads
        1. What just happened?
      8. Time for action - Creating a specialized parallel algorithm coordination subclass to run concurrently with the pool of threads
        1. What just happened?
      9. Time for action - Overriding methods in the brightness adjustment coordination subclass
        1. What just happened?
      10. Time for action - Starting new threads in a new window
        1. What just happened?
        2. Creating threads inside other threads
      11. Time for action - Showing new windows without blocking the user interface
        1. What just happened?
        2. Multiple windows and one UI thread for all of them
      12. Rationalizing multithreaded code
        1. Have a go hero - Improving the application and solving bugs
        2. Have a go hero - Creating parallel, multithreaded applications using the C# programming language
        3. Pop quiz
      13. Summary