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

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

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

Beginner's Guide
Gastón C. Hillar

Exploit the power of multiple processors for faster, more responsive software.
$23.99
$39.99
RRP $23.99
RRP $39.99
eBook
Print + eBook
$12.99 p/month

Get Access

Get Unlimited Access to every Packt eBook and Video course

Enjoy full and instant access to over 3000 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 139781847197108
Paperback416 pages

About This Book

  • 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

Who This Book Is For

Whether you are a beginner to working with threads or an old hand that is looking for a reference, this book should be on your desk. This book will help you to build scalable, high performance software using parallel programming techniques.

Students learning introductory threaded programming in C# will also gain benefits from this book.

Table of Contents

Chapter 1: 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
Chapter 2: Processes and Threads
Processes—any running program
Threads—Independent parts of a process
Summary
Chapter 3: BackgroundWorker—Putting Threads to Work
RTC: Rapid thread creation
Asynchronous execution
Synchronous execution
Showing the progress
Cancelling the job
Working with multiple BackgroundWorker components
BackgroundWorker and Timer
BackgroundWorker creation on the fly
Summary
Chapter 4: Thread Class—Practical Multithreading in Applications
Creating threads with the Thread class
Sharing data between threads
Passing parameters to threads
Summary
Chapter 5: Simple Debugging Techniques with Multithreading
Watching multiple threads
Debugging multithreaded applications as single-threaded applications
Showing partial results in multithreaded code
Summary
Chapter 6: Understanding Thread Control with Patterns
Starting, joining, pausing, and restarting threads
Understanding the pixels' color compositions
Creating independent blocks of concurrent code
Summary
Chapter 7: Dynamically Splitting Jobs into Pieces—Avoiding Problems
Running split jobs many times
Generalizing the algorithms for segmentation with classes
Summary
Chapter 8: Simplifying Parallelism Complexity
Specializing the algorithms for segmentation with classes
Avoiding locks and many synchronization nightmares
Summary
Chapter 9: Working with Parallelized Input/Output and Data Access
Queuing threads with I/O operations
Parallelizing database access
Summary
Chapter 10: Parallelizing and Concurrently Updating the User Interface
Updating the UI from independent threads
Reporting progress to the UI from independent threads
Summary
Chapter 11: Coding with .NET Parallel Extensions
Parallelizing loops using .NET extensions
Parallelizing queries
Summary
Chapter 12: Developing a Completely Parallelized Application
Joining many different parallelized pieces into a complete application
Rationalizing multithreaded code
Summary

What You Will Learn

  • Transform single-threaded algorithms to multithreaded scalable jobs by using object-oriented capabilities create an independent thread with an asynchronous execution by using a delegate to update the UI
  • Achieve responsive user interface using synchronous and asynchronous delegates to dispatch changes to the UI thread
  • Specify the degree of parallelism desired for determining the number of threads that the query must use to achieve better performance
  • Create applications that use the full power of the processors using parallel programming techniques
  • Develop applications to create background threads using the BackgroundWorker component
  • Learn debugging techniques to find solutions to multithreaded application problems

Chapter 1: Taking Advantage of Multiprocessing and Multiple Cores
Mono-processor systems: The old gladiators
Single core: Only one warrior to fight against everybody
The performance waterfall
Multi-processor systems: Many warriors to win a battle
Estimating performance improvements
Avoiding bottlenecks
Taking advantage of multiple execution cores
Scalability
Load balancing: Keeping everybody happy
Operating systems and virtual machines
Parallelism is here to stay
Code patterns learned in this chapter
Exercises

Chapter 2: Processes and Threads
What is a process?
What is a thread?
Multiple threads in servers
Multiple threads in clients
Code patterns learned in this chapter
Exercises

Chapter 3: BackgroundWorker in action for beginners
RTC: Rapid thread creation
Defining the work to be done
Asynchronous execution
Synchronous execution
Showing the progress
Canceling the job
Working with multiple BackgroundWorker
BackgroundWorker vs. Timer
BackgroundWorker creation on the fly
Code patterns learned in this chapter
Exercises

Chapter 4: Thread Class—Practical Multithreading in Applications
Creating threads with the Thread class
Sharing data between threads
Passing parameters to threads
Code patterns learned in this chapter
Exercises

Chapter 5: Simple Debugging Techniques with Multithreading
Watching multiple threads
Debugging multithreaded applications as single-threaded applications
Showing partial results in multi threaded code
Code patterns learned in this chapter
Exercises

Chapter 6: Understanding threads’ control with patterns
Starting, Joining, Pausing and Restarting threads
Understanding the pixels' color compositions
Creating independent blocks of concurrent code
Using flags to enhance control over concurrent threads
Testing results with Performance Monitor and Process Explorer
Using the AutoResetEvent class to handle signals between threads
Using the WaitHandle class to check for signals
Code patterns learned in this chapter
Exercises

Chapter 7: Dynamically Splitting Jobs into Pieces—Avoiding Problems
Running split jobs many times
Using classes, methods, procedures, and functions with multithreading capabilities
Understanding the garbage collector with multi threading
Controlling the system garbage collector with the GC class
Avoiding garbage collection problems
Avoiding inefficient processing usage problems
Retrieving the total memory thought to be allocated
Generalizing the algorithms for segmentation with classes
Code patterns learned in this chapter
Exercises

Chapter 8: Simplifying Parallelism Complexity
Specializing the algorithms for segmentation with classes
Defining the class to instantiate
Creating simple constructors
Programming the results collection method
Optimizing and encapsulati ng parallel algorithms
Achieving thread affinity
Avoiding locks and many synchronization nightmares
Code patterns learned in this chapter
Exercises

Chapter 9: Working with Parallelized Input/Output and Data Access
Queuing threads with I/O operati ons
Avoiding Input/Output bott lenecks
Using concurrent streams
Controlling excepti ons in threads
Using a pool of threads with the ThreadPool class
Managing the thread queue in the pool
Converting single-threaded tasks to a multithreaded pool
Encapsulating scalability
Thread affinity in a pool of threads
Parallelizing database access
Code patterns learned in this chapter
Exercises

Chapter 10: Parallelizing and Concurrently Updating the User Interface
Updating the UI from independent threads
Creating delegates to make cross-thread calls
Reporting progress to the UI from independent threads
Creating a delegate without parameters
Invoking a delegate asynchronously to avoid performance degradation
Code patterns learned in this chapter
Exercises

Chapter 11: Coding with .NET Parallel Extensions
Parallelizing loops using .NET extensions
Counting and showing blobs while avoiding side effects
Using a parallelized ForEach loop
Coding with delegates in parallelized loops
Working with a concurrent queue
Controlling excepti ons in parallelized loops
Combining delegates with a BackgroundWorker
Retrieving elements from a concurrent queue in a producer-consumer scheme
Creating an asynchronous task combined with a synchronous parallel loop
Using lambda expressions to simplify the code
Parallelizing loops with ranges
Parallelizing queries
Parallelizing LINQ queries with PLINQ
Specifying the degree of parallelism for PLINQ
Parallelizing stati stics and multiple queries
Code patterns learned in this chapter
Exercises

Chapter 12: Developing a Completely Parallelized Application
Joining many different parallelized pieces into a complete application
Running code out of the UI thread
Blocking the UI—Forbidden with multi threading code
Combining threads with a pool of threads and the UI thread
Creating threads inside other threads
Multi ple windows and one UI thread for all of them
Rationalizing multi threaded code
Code patterns learned in this chapter
Exercises

In Detail

Most modern machines have dual core processors. This means that multitasking is built right into your computer's 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.

Authors

Table of Contents

Chapter 1: 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
Chapter 2: Processes and Threads
Processes—any running program
Threads—Independent parts of a process
Summary
Chapter 3: BackgroundWorker—Putting Threads to Work
RTC: Rapid thread creation
Asynchronous execution
Synchronous execution
Showing the progress
Cancelling the job
Working with multiple BackgroundWorker components
BackgroundWorker and Timer
BackgroundWorker creation on the fly
Summary
Chapter 4: Thread Class—Practical Multithreading in Applications
Creating threads with the Thread class
Sharing data between threads
Passing parameters to threads
Summary
Chapter 5: Simple Debugging Techniques with Multithreading
Watching multiple threads
Debugging multithreaded applications as single-threaded applications
Showing partial results in multithreaded code
Summary
Chapter 6: Understanding Thread Control with Patterns
Starting, joining, pausing, and restarting threads
Understanding the pixels' color compositions
Creating independent blocks of concurrent code
Summary
Chapter 7: Dynamically Splitting Jobs into Pieces—Avoiding Problems
Running split jobs many times
Generalizing the algorithms for segmentation with classes
Summary
Chapter 8: Simplifying Parallelism Complexity
Specializing the algorithms for segmentation with classes
Avoiding locks and many synchronization nightmares
Summary
Chapter 9: Working with Parallelized Input/Output and Data Access
Queuing threads with I/O operations
Parallelizing database access
Summary
Chapter 10: Parallelizing and Concurrently Updating the User Interface
Updating the UI from independent threads
Reporting progress to the UI from independent threads
Summary
Chapter 11: Coding with .NET Parallel Extensions
Parallelizing loops using .NET extensions
Parallelizing queries
Summary
Chapter 12: Developing a Completely Parallelized Application
Joining many different parallelized pieces into a complete application
Rationalizing multithreaded code
Summary

Book Details

ISBN 139781847197108
Paperback416 pages
Read More