1. Technology

C# 2008 and 2005 Threaded Programming

Threaded Programming Isn't the Simplest

About.com Rating 5 Star Rating

By

C# 2008 and 2005 Threaded Programming
Multi-threaded programming is one of the most complicated parts of programming, and it's not going away. Computers are getting faster not by having faster processors but more cores. You have to think slightly differently; how data is stored so it can be accessed by different threads. How threads interact etc.

For C# programming, this book shows you how to deal with these problems.

Who is the Book Aimed at?

This book goes into quite extraordinary depth to explain multi-threaded programming and there's no doubt that any developer who is past the raw beginner stage would benefit from it. Chapter one is all theory but explained in a very clear and practical way and chapter two then jumps straight into code.

I don't know all the components and controls on the C# Toolbar but one I'd never noticed until I read the book was the BackgroundWorker; it's an easy way to run multiple threads in one app. There's a demo of what happens to GUIs when you run heavy processing in the main thread and it freezes the UI. BackgroundWorkers are then used to keep the UI responsive and delegates are touched on but covered in a later chapter.

By the time you've got through the third chapter, it's been fairly easy with the first 100 pages setting the scene for the more complicated code that's not far away. Chapter four is where the learning curve gets a little bit steeper.

Practical Multi-threading 101

Beyond the simple but useful BackgroundWorker component lies the Thread class and this is where the real complexity starts to kick in. Some of the examples use C# generics but nothing too hard. There's a lot to take in here and by the end of the chapter you'll have covered a writing applications that run faster on multiple cores as well as the minutiae of controlling threads, passing in parameters etc.

This is actually 75-80% most of what you'll ever need to know about threading but we're only 1/3rd of the way through the book and the rest is the interesting stuff. Chapter five covers debugging for both single-threaded and multi-threaded applications and shows how to use breakpoints and tracepoints.

The next chapter builds on what's been learnt with an image processing application that splits the job into lots of small images and processes each in its own thread. This is building towards chapter seven where a large numbers of images are processed and the problem of dealing with garbage collection in a multi-threaded application by forcing the garbage collector to be run. There's also a good bit of content in this chapter, on architecting multi-threaded applications to simplify development.

Parallelism and Complexity

The last five chapters continue both simplifying the architecture and hiding the thread code in a simple to use design. Chapter nine looks at I/O and the ThreadPool class and examines ways to avoid bottlenecks and concurrency problems.

In chapter ten, the use of delegates and the Model View Controller (MVC) to make updating the user interface (UI) more snappy is discussed. This is a complicated subject when there are multiple threads all trying to talk to the single UI thread.

Chapter eleven looks at the Microsoft .NET Parallel Extensions library which are Microsoft's take on simplifying parallel computing and will be part of .NET 4.0. The current release for 3.5 is a CTP (Community technology Preview), not the final release. For some developers, keen to use concurrent queue classes or parallelized foreach this could make the whole book worthwhile. It includes a working application.

In the last chapter I think the author is showing off a little (he has deserved it) having an image processing application in a Window that runs even while the window is resized or minimized etc. It's not a bad way to finish the book. If you've worked your way through it all, you'll be able to do that!

In Conclusion

If you are going to use threading in your application and want to make it responsive and robust then this book is a must have. It touches on rare coding situations that I've had painful experiences and impressed me greatly.

The author really knows his stuff and while some of the book is best described as a little corny (the bit about the killer guards, the FBI, Nasa!), it doesn't detract from the meat of the book and it rightly deserves the 5 star rating.

Some books are padded out with lots of code but that's not the case here; there are snippets and longer bits all the way through but no full listings so if you want to study his code you do need to download the 50MB file from the publisher's website.

Minor criticism. It assumes a certain level of knowledge on things like the difference between single threaded apartments and multi- threaded apartments that many developers may not have come across. A little explanation of that would add a little polish.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Book and Software Reviews
  5. C# 2008 and 2005 Threaded Programming Book review

©2014 About.com. All rights reserved.