Deep C# - Threading,Tasks and Locking |
Written by Mike James | ||||||||
Wednesday, 22 October 2025 | ||||||||
Page 1 of 7 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 Chapter List
Extra Material <ASIN:B09FTLPTP9> Threading, Tasks & LockingA 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 ) |