.NET Multithreading
Alan Dennis
  • November 2002
  • ISBN 9781930110540
  • 360 pages
.NET Multithreading
Alan Dennis

If you need high performance, or a rich user experience, you should consider multithreading. With .NET you can develop stable and robust multithreaded applications with minimal effort. .NET Multithreading teaches the basics in an understandable and practical way. It then focuses on .NET's mechanisms for multithreading and shows how easy it can be to develop applications with them. The book covers several design approaches such as one-thread-one-class, the asynchronous design pattern, and using queues as buffers between threads. It explains best practices and how to avoid common multithreading pitfalls such as deadlock and race conditions.

Table of Contents detailed table of contents

1. Process and Thread basics

1.1. What is a process, why do they exist?

1.2. Multitasking

1.2.1. Cooperative

1.2.2. Preemptive

1.3. How Preemptive Multitasking is Accomplished

1.3.1. What are threads and why should we care

1.3.2. Time slice and quantum

1.3.3. Context and Context switching

1.4. Summary

2. .NET from a Threading Perspective

2.1. .NET Architecture Overview

2.1.1. Framework Runtime

2.1.2. Class Libraries

2.1.3. ASP .NET

2.1.4. Custom Libraries

2.1.5. Managed Applications

2.2. Garbage Collection

2.2.1. Review of Garbage Collection

2.2.2. Finalization

2.3. Security

2.4. Summary

3. Multithreading in .Net

3.1. Application Domain

3.1.1. Compared to a Process

3.1.2. AppDomain

3.1.3. CreateDomain

3.2. Threads

3.2.1. Logical

Threading Namespace
Thread Class
ThreadStart delegate

3.2.2. Physical


3.3. Summary

4. Thread Lifecycle

4.1. Creating a new thread

4.1.1. Defining the thread’s entry point

4.1.2. Creating an instance of the ThreadStart delegate

4.1.3. Creating an Instance of the Thread class

4.2. Starting a thread

4.3. Ending a thread

4.3.1. Introduction to thread Exceptions

4.3.2. ResetAbort

4.3.3. Join - Waiting on a thread to exit

4.4. Determining a Thread’s Status

4.4.1. IsAlive

4.4.2. ThreadState

4.5. Summary

5. Controlling threads

5.1. Example: Web Site Monitoring

5.2. Naming Threads

5.3. Using Sleep and Interrupt

5.3.1. The Sleep Method

5.3.2. The Interrupt Method

5.4. Using Background and Foreground Threads

5.5. Using Suspend and Resume

5.5.1. The Suspend Method

5.5.2. The Resume Method

5.6. Exploring Thread States

5.7. Digging Deeper into Thread Control

5.7.1. Controlling Thread Priority

5.7.2. Setting Process Processor Affinity

5.7.3. Specifying an Ideal Processor

5.8. Summary

6. Communicating with threads

6.1. Using Data to Communicate

6.1.1. Public fields

6.1.2. Public properties

6.1.3. Queues and Threads

6.2. When things go badly

6.2.1. Race conditions

6.2.2. Deadlock

6.3. Summary

7. Concurrency Control

7.1. Understanding Thread-Safety

7.1.1. Race conditions in collections

7.1.2. Making collections thread-safe using the Synchronized method

7.1.3. Thread-Safety in Libraries

7.1.4. Understanding and Detecting Thread Boundaries

7.2. Atomic operations

7.2.1. The Interlocked Class

Decrement, Increment
Exchange, and CompareExchange

7.3. Lock and AsyncLock Statements

7.4. The Monitor Class

7.4.1. The Enter and Exit Methods

Comparing to synchronous locks
Creating critical sections

7.4.2. The TryEnter Method

7.4.3. The Wait and Pulse Methods

7.4.4. The PulseAll Method

7.5. Digging Deeper into Concurrency Control

7.5.1. The Volatile Keyword

7.5.2. Using COM+ Based Synchronization

7.6. Summary

8. Wait Handle Classes

8.1. An Overview of the WaitHandle class

8.2. AutoResetEvent:

8.2.1. Set

8.2.2. Reset

8.3. WaitHandle

8.3.1. WaitOne

8.3.2. WaitAll

8.3.3. WaitAny

8.4. ManualResetEvent

8.5. Mutex: WaitOne and ReleaseMutex

8.6. Summary

9. Reader Writer Lock

9.1. Reader Lock

9.1.1. Acquire and Release Reader Lock

9.1.2. IsReaderLockHeld

9.2. Writer Lock

9.2.1. Acquire, Release, and Is Lock Held

9.2.2. UpgradeToWriterLock

9.2.3. DowngradeFromWriterLock

9.2.4. WriterSeqNum and AnyWritersSince

9.3. ReleaseLock and RestoreLock

9.4. Summary

10. Thread Pool

10.1. ThreadPool Class and QueueUserWorkItem

10.2. RegisterWaitForSingleObject

10.3. Informational Methods and Properties

10.3.1. The GetMaxThreads and GetAvailableThreads Methods

10.3.2. The IsThreadPoolThread Property

10.4. The UnsafeRegisterWaitForSingleObject and UnsafeQueueUserWorkItem Methods

10.5. What ThreadPools are used for in .NET

10.6. Summary

11. ThreadStatic and Thread Local Storage

11.1. Using Thread Static Variables

11.2. Using Unnamed Data Slots

11.3. Using Named Data Slots

11.4. Freeing Named Data Slots

11.5. Summary

12. Delegates in Detail

12.1. Delegates Revisited

12.2. The ThreadStart Delegate

12.3. Callbacks

12.3.1. TimerCallback

12.3.2. WaitCallback

12.3.3. WaitOrTimerCallback

12.4. Handling Thread Exceptions in Windows Forms

12.5. Asynchronous Delegates

12.5.1. EndInvoke

12.5.2. AsyncCallback

12.6. Creating and Invoking Dynamic Delegates

12.7. Summary

13. Exceptions

13.1. Exceptions Revisited

13.2.1. The ThreadAbortException Class

13.2.2. The ThreadInterruptedException Class

13.2.3. The ThreadStateException Class

13.2.4. The SynchronizationLockException Class

13.3. The AppDomain UnhandledException Event

13.4. Summary

14. Timers

14.1. Using Windows Forms Timers

14.1.1. Examining the System.Windows.Forms.Timer Class

14.1.2. Controlling Windows Forms Timers

14.2. System.Timers.Timer

14.2.1. Using in Windows Forms

14.2.2. Using in System Services

14.3. System.Threading.Timer

14.4. Summary

15. Windows Forms from a Multithreading Perspective

15.1.1. Introduction to the STAThread Attribute

15.1.3. Race Conditions


15.1.5. Making Windows Forms Thread Safe


15.2. Using the Graphics Object With Threads

15.2.1. Introduction to the Graphics object

Acquiring by overriding the OnPaint method
Acquiring using the FromHwnd method

15.3.1. ThreadException Event

15.3.2. ThreadExit Event

15.3.3. The MessageLoop Property

15.4. Summary

16. Unmanaged Code and Managed Threads

16.1. What exactly is an apartment?

16.1.1. Single Threaded Apartment Model (STA)

16.1.2. Multithreaded Apartment (MTA)

16.2. COM Interoperability

16.2.1. The ApartmentState Property

16.2.2. Apartment Conflicts

16.2.3. Discussion of the Example

16.3. Summary

17. Designing with Threads

17.1. The Asynchronous Design Pattern

17.1.1. File Sorting Example

17.1.2. The Sorter Class Library

The WorkUnit Class
Sorter Class
The "heart" of the Sorter class

17.1.3. Using the Sorter class

Synchronous Execution of Sort
Event Handlers
Asynchronous Execution of Sort

17.1.4. Steps to Follow the Asynchronous Design Pattern

17.2. MessageQueue Example

17.2.1. Message Producer

17.2.2. Message Consumer

Synchronous Receive
Asynchronous Processing using BeginReceive
Using BeginReceive with a callback

17.3. One Class One Thread

17.4. Performance issues

17.4.1. Multithreading overhead

17.4.2. Increasing Concurrency

17.4.3. Implications of multiple processors

17.5. Summary

18. Multithreading in J#

What's inside

  • Creating multithreaded System Services
  • Designing for high performance
  • Using Asynchronous Delegates
  • How to control thread execution
  • Handling and logging errors
  • How to protect data from corruption
  • How to coordinate threads
  • How to use ThreadPools effectively

About the reader

This book is written for intermediate .NET developers who know C# or VB .NET, but are not assumed to have a background in multithreading. It is rich in examples that will help you understand the subject and produce multithreaded applications that have the power of C++ while keeping the ease and reliability of .NET.

About the author

Alan Dennis is a Microsoft-certified solution developer who has developed numerous multithreaded applications using C++, Java, and .NET. He lives in Grapevine, Texas.

.NET Multithreading (eBook) added to cart
continue shopping
go to cart

eBook $17.50 PDF only

FREE domestic shipping on three or more pBooks