Deep C# - Threading,Tasks and Locking
Written by Mike James   
Wednesday, 22 October 2025
Article Index
Deep C# - Threading,Tasks and Locking
Starting A Thread
Ensuring Thread Safety
Exclusion Using Locking
Deadlock
Background Worker
Task Parameters

Threading in C# is basic to implementing asychronous code and it is easy to get started with but also easy to make a mess of. Find out more in this extract from my book, Deep C#: Dive Into Modern C#.

Deep C#

 Buy Now From Amazon

DeepCsharp360

 Chapter List

  1. Why C#?
    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
       Extract
    Value And Reference
  4. Structs & Classes
       Extract
    Structs & Classes 
  5. Inheritance
      
    Extract
    Inheritance
  6. Interfaces & Multiple Inheritance
      
    Extract Interface
  7. Controlling Inheritance
    II Casting & Generics
  8. Casting - The Escape From Strong Typing
      
    Extract Casting I
  9. Generics
  10. Advanced Generics
  11. Anonymous & Dynamic
    Typing
    III Functions
  12. Delegates 
  13. Multicast Delegates
  14. Anonymous Methods, Lambdas & Closures
    IV Async
  15. Threading,Tasks and Locking ***NEW!
  16. The Invoke Pattern
  17. Async Await
  18. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  19. The LINQ Principle
  20. XML
  21. LINQ To XML
  22. Regular Expressions
    VI Unsafe & Interop
  23. Interop
  24. COM
  25. Custom Attributes
  26. Bit Manipulation
  27. Advanced Structs
  28. Pointers 

Extra Material
Passing Parameters ***NEW!

 <ASIN:B09FTLPTP9>

Threading, Tasks & Locking

A thread of execution is just that - a set of instructions that are carried out more or less independently of any other set of instructions.

The simplest and most common programming model is single threading. In .NET every GUI program has a single UI thread of execution which responds to all of the events generated by the user. The majority of programs really don’t need anything more than this single thread of execution, but occasionally there is something to be gained in using more than one thread.

Generally speaking multi-threading can make a program more responsive to the user by splitting off worker threads that don’t block the UI thread. Multi-threading can make a program actually run faster, although this is a much less common situation than you might think. The only gains in efficiency that are possible are if a single thread is repeatedly blocked waiting for something to happen and there is work that another thread could be getting on with. In this case, however, the single-threaded version of the program could also be made more efficient by simply getting it to do work while it would otherwise be idle.

Until quite recently most multi-threading was simulated by the operating system switching a single processor’s attention between a set of active threads. In this case there is only one thread actually being obeyed at any one time, but the rate of switching between threads is sufficiently high to give the impression that all of the threads are being run. Simulating multi-threading is generally done by preemptive multi-tasking, but there are other ways of doing it.

With the rise of multi-core processors the possibility of real multi-threading, albeit limited to a small number of threads, has become possible and in this case the potential for speed increase is more real. All in all threading is becoming increasingly important. .NET makes the creation of multi-threaded programs very easy and while this can be considered a good thing it also has some negative consequences.

Using multiple threads properly is a very difficult and subtle task and not to be undertaken lightly. So while it is good that .NET facilitates their use, it should not be taken as an encouragement to proliferate threads without some careful thought. Here we explore how multi-threading works and, more importantly, how it can go very wrong. In particular, we take a look at some of the more obscure ways in which threading can become part of your program via Invoke and the background worker class, but first some standard threading.



Last Updated ( Wednesday, 22 October 2025 )