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


C# 2008 and 2005 Threaded Programming: Beginner's Guide
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
$20.40
save 15%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
$60.39
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Reviews
Support
Sample Chapters
  • 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

Book Details

Language : English
Paperback : 416 pages [ 235mm x 191mm ]
Release Date : January 2009
ISBN : 1847197108
ISBN 13 : 9781847197108
Author(s) : Gastón C. Hillar
Topics and Technologies : All Books, Application Development, Microsoft Other, Beginner's Guides, Microsoft, .NET


Table of Contents

Preface
Chapter 1: Taking Advantage of Multiprocessing and Multiple Cores
Chapter 2: Processes and Threads
Chapter 3: BackgroundWorker—Putting Threads to Work
Chapter 4: Thread Class—Practical Multithreading in Applications
Chapter 5: Simple Debugging Techniques with Multithreading
Chapter 6: Understanding Thread Control with Patterns
Chapter 7: Dynamically Splitting Jobs into Pieces—Avoiding Problems
Chapter 8: Simplifying Parallelism Complexity
Chapter 9: Working with Parallelized Input/Output and Data Access
Chapter 10: Parallelizing and Concurrently Updating the User Interface
Chapter 11: Coding with .NET Parallel Extensions
Chapter 12: Developing a Completely Parallelized Application
Index
  • Chapter 1: Taking Advantage of Multiprocessing and Multiple Cores
    • Mono-processor systems: The old gladiators
      • Single core: Only one warrior to fight against everybody
      • Doing a tiny bit of each task
    • 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
    • Summary
  • Chapter 2: 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
    • Relating processes to cores
      • Time for action – Changing a process priority
    • Linear code problems in multiprocessing systems
      • 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
  • Multiple threads in servers
  • Multiple threads in clients
  • Summary
  • 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
  • Sharing data between threads
    • Time for action – Updating the UI while running threads
  • Sharing some specific data between threads
  • A BackgroundWorker helping a Thread class
    • Time for action – Executing the thread synchronously
  • Main and secondary threads
  • 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
  • Creating as many threads as the number of cores
  • Receiving parameters in the thread method
  • Summary
  • Chapter 5: Simple Debugging Techniques with Multithreading
    • Watching multiple threads
      • Time for action – Understanding the difficulty in debugging
      • concurrent threads
    • Debugging concurrent threads
      • Time for action – Finding the threads
    • Understanding the information shown in the Threads window
      • Time for action – Assigning names to threads
    • Identifying the current thread at runtime
  • Debugging multithreaded applications as single-threaded applications
    • Time for action – Leaving a thread running alone
  • Freezing and thawing threads
  • Viewing the call stack for each running thread
  • Showing partial results in multithreaded code
    • Time for action – Explaining the encryption procedure
  • Showing thread-safe output
    • Time for action – Isolating results
  • Understanding thread information in tracepoints
  • Summary
  • Understanding the pixels' color compositions
    • Time for action – Running the stars counter in many concurrent threads
  • Creating independent blocks of concurrent code
    • Using flags to enhance control over concurrent threads
    • Rebuilding results to show in the UI
    • Testing results with Performance Monitor and Process Explorer
      • Time for action –Waiting for the threads' signals
    • Using the AutoResetEvent class to handle signals between threads
    • Using the WaitHandle class to check for signals
  • Summary
  • Chapter 7: 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
    • Using classes, methods, procedures, and functions with multithreading capabilities
      • Time for action – Analyzing the memory usage
    • Understanding the garbage collector with multithreading
      • Time for action – Collecting the garbage at the right time
    • 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
    • Time for action – Creating a parallel algorithm piece class
    • Time for action – Using a generic method in order to create pieces
  • Creating the pieces
    • Time for action – Creating a parallel algorithm coordination class
  • Starting the threads associated to the pieces
  • Accessing instances and variables from threads' methods
    • Time for action – Adding useful classic coordination methods
  • Summary
  • Chapter 8: Simplifying Parallelism Complexity
    • Specializing the algorithms for segmentation with classes
      • Time for action – Preparing the parallel algorithm classes for the
      • factory method
    • Defining the class to instantiate
    • Preparing the classes for inheritance
      • Time for action – Creating a specialized parallel algorithm piece subclass
    • Creating a complete piece of work
    • Writing the code for a thread in an instance method
      • Time for action – Creating a specialized parallel algorithm
      • coordination subclass
    • Creating simple constructors
      • Time for action—Overriding methods in the coordination subclass
      • Programming the piece creation method
    • Programming the results collection method
      • Time for action – Defining a new method to create an algorithm instance
    • Forgetting about threads
      • Time for action – Running the Sunspot Analyzer in many concurrent
      • independent pieces
    • Optimizing and encapsulating parallel algorithms
    • Achieving thread affinity
  • Avoiding locks and many synchronization nightmares
  • Summary
  • Chapter 9: 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
    • Avoiding Input/Output bottlenecks
    • Using concurrent streams
    • Controlling exceptions in threads
      • Time for action – Creating the methods for queuing requests
    • Using a pool of threads with the ThreadPool class
    • Managing the thread queue in the pool
      • Time for action – Running concurrent encryptions on demand using a
      • pool of threads
    • Converting single-threaded tasks to a multithreaded pool
    • Encapsulating scalability
    • Thread affinity in a pool of threads
  • Parallelizing database access
  • Summary
  • Chapter 10: 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
    • Creating delegates to make cross-thread calls
    • Figuring out the right thread to make the call to the UI
    • Avoiding UI update problems with a delegate
    • Retrieving results from a synchronous delegate invoke
      • Time for action – Invoking a user interface update from a thread
    • Providing feedback when the work is finished
      • Time for action – Identifying threads and giving them names
      • Time for action – Understanding how to invoke delegates step-by-step
    • Decoding the delegates and concurrency puzzle
      • Time for action – Creating safe counters using delegates and avoiding
      • concurrency problems
    • Taking advantage of the single-threaded UI to create safe counters
  • 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
  • Creating a delegate without parameters
  • Invoking a delegate asynchronously to avoid performance degradation
    • Time for action – Updating progress percentages from worker threads
  • Providing feedback while the work is being done
  • Summary
  • Chapter 11: Coding with .NET Parallel Extensions
    • Parallelizing loops using .NET extensions
      • Time for action – Downloading and installing the .NET Parallel Extensions
    • No silver bullet
      • Time for action – Downloading and installing the imaging library
      • Time for action – Creating an independent class to run in parallel without
      • side effects
    • Counting and showing blobs while avoiding side effects
      • Time for action – Running concurrent nebula finders using a
      • parallelized loop
    • Using a parallelized ForEach loop
    • Coding with delegates in parallelized loops
    • Working with a concurrent queue
    • Controlling exceptions in parallelized loops
      • Time for action – Showing the results in the UI
    • Combining delegates with a BackgroundWorker
    • Retrieving elements from a concurrent queue in a producer-consumer scheme
      • Time for action – Providing feedback to the UI using a producer-consumer
      • scheme
    • Creating an asynchronous task combined with a synchronous parallel loop
      • Time for action – Invoking a UI update from a task
    • Providing feedback when each job is finished
    • Using lambda expressions to simplify the code
    • Parallelizing loops with ranges
  • Parallelizing queries
    • Time for action – Parallelized counter
  • Parallelizing LINQ queries with PLINQ
  • Specifying the degree of parallelism for PLINQ
  • Parallelizing statistics and multiple queries
  • Summary
  • Chapter 12: 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
    • Running code out of the UI thread
      • Time for action – Creating a safe method to change the opacity
    • Blocking the UI—Forbidden with multithreading code
      • 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
    • Combining threads with a pool of threads and the UI thread
      • 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
    • Creating threads inside other threads
      • Time for action – Showing new windows without blocking the user
      • interface
    • Multiple windows and one UI thread for all of them
  • Rationalizing multithreaded code
  • Summary

Gastón C. Hillar

Gastón C. Hillar has been working with computers since he was eight. He began programming with the legendary Texas TI-99/4A and Commodore 64 home computers in the early 80s. He has a Bachelor degree in Computer Science from which he graduated with honors, and an MBA from which he graduated with an outstanding thesis. Now, he is an independent IT consultant and a freelance author always looking for new adventures around the world. To date he’s written more than 40 books in Spanish, and for Packt Publishing has written “C# 2008 and 2005 Threaded Programming: Beginner's Guide”. He usually writes articles for Spanish magazines Mundo Linux, Solo Programadores and Resistor. He contributes to Dr. Dobb's Go Parallel programming portal http://www.ddj.com/go-parallel/ and he is a guest blogger at Intel Software Network http://software.intel.com Gastón C. Hillar is the author of "Microsoft Silverlight 4 and SharePoint 2010 Integration".

Code Downloads

Download the code and support files for this book.


Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


Errata

- 3 submitted: last submission 03 Oct 2013

Errata type: Typo | Page number: 1

First word in the last sentence should be spelled "After" instead of "Afer".

Errata type: Graphics | Page number: 19

The two graphs should be swapped.

Errata type: Code | Page number: 117

Before running step number 5, this step should be run:
Add the following method to encrypt the lines in stored in prlsSMSToEncrypt:
private void ThreadEncryptProcedure2()
{
string lsEncryptedText;
prlsEncryptedSMS = new List(prlsSMSToEncrypt.Count);
int i;
string lsText; // Iterate through each string in the prlsSMSToEncrypt string list
for (i = 0; i < prlsSMSToEncrypt.Count; i++)
{
lsText = prlsSMSToEncrypt[i]; lsEncryptedText = Encrypt(lsText);
// Append a string with the Encrypted text
prlsEncryptedSMS.Add(lsEncryptedText);
}
}

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

C# 2008 and 2005 Threaded Programming: Beginner's Guide +    Activiti 5.x Business Process Management Beginner's Guide =
50% Off
the second eBook
Price for both: $37.50

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • 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.

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

Approach

This is a concise practical guide that will help you learn C# threaded programming, with lot of examples and clear explanations. It is packed with screenshots to aid your understanding of the process.

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.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software