views:

361

answers:

3

Can you recommend a good book -- or other learning resource -- for using C# to build Windows Services that are multi-threaded?

Specifically, I am using Visual Studio 2008 with .Net Framework 3.5. The books that I am finding deal with web services. Further, I am finding next to nothing on multi-threading.

Thanks in advance,

Wolfgang

A: 

Your best bet is probably to read about building Windows Services and then read about multi threading. I doubt you'll find something that combines both in much detail.

I'm not sure about the Windows Services, but I found Troelsen's Pro C# (apress) book very good for multi-threading.

AndyC
+1  A: 

I spend a fair amount of my time building multi-threaded Windows Services and although I don't know of any books specific to that I can offer some suggestions:

  • Package your worker classes as a separate project and wring out bugs and performance issues using a WPF or WinForm app, where debugging is a lot more convenient. Then you can invoke it from a service with some measure of confidence.
  • Use logging liberally. I like to log to a database, wrapping inserts in transactions to prevent contention and get single-filed log entries. It also makes it easy to view logs from other applications. If you don't want a big database engine, http://sqlite.phxsoftware.com/ is an excellent open-source ADO.Net wrapper around Sqlite. Include some SQL in your insert procedure to remove old entries after 30 days or so.
  • Use a supervisor class to monitor and restart worker threads so that exceptions don't halt the service. Catch all exceptions in the worker class so that it can gracefully tell the supervisor that it exited abnormally. Provide a way for the supervisor to signal the worker to shut down gracefully, too. For one app I use a separate thread to queue log requests and write them to the database so that the other worker threads can keep going. You might even want two tables: a high-level one for monitoring and a maddeningly detailed one for debugging.
  • Plan how to communicate with the service to control and monitor the threads. You can use a control table in your database or use MSMQ. Same goes for configuration data. If you can tell the supervisor to start and stop, then you can change configuration data remotely without having to restart the actual service.
  • Include an option to send an email or SMS message for critical errors.
  • Consider using a self-hosted WCF service as your Windows service. Then you can use web services for configuration and control.
ebpower
A: 

This book doesn't seem to have been mentioned yet, but Joe Duffy's "Concurrent Programming on Windows" is just about as good as it gets in my opinion. It's basically my concurrency bible. Can't recommend it highly enough. Give it a read!

edit: to provide a little more detail, it provides deep insight into native windows APIs, managed APIs available through .net, and also provides details into the differences between the threading facilities in several major versions of windows. However, having been written before the release of 7, the most recent version is covers is Vista--although I'm not sure W7 is much different. There's substantial theory and history there, as well as several different applications. It's long, but in the long run if you're invested in becoming a skilled concurrent programmer I believe this to be a feature and not a flaw.

bwerks