Multi-threading Issues
Here we look at multi-threading issues and some examples of how to manage and work with multi-threaded applications.
Synchronised Generic Dictionary
Here is an example of a synchronised generic dictionary. This class is completely thread-safe and has been tested. Please read the notes at the top of the link about why SyncRoot was removed and consider these in your use of the class, espically when multiple resources need to be synchronised.
Using the Interlocked classes to allow only a single thread access to a code block
Using SyncLock (or Monitor in C#) to block all threads accessing a block of code can be overkill. SyncLock requires that all CPU instructions be cleared from the registers so that thread synchronisation can be performed safely and it also requires a lot of management by the .NET threading libraries.
In certain situation where multiple threads are running, you may require that just a single thread execute a piece of code at any one time. I've used this in situations where multiple threads are there for performance reasons but only 1 thread at a time should say clear down/refresh cache entries, but any of those threads could do the work.
Here is the an example of using such a technique.
Capturing and analyzing output from launched application
It is useful in lots of places to be able to capture and display the output from an application that was launched and base decisions on that output or else display process to the user based upon that output. Here is a fully working example of such a class. There are a number of threading issues covered in this example.
Using Lock files for simple interprocess co-ordination mechanism
Here is a sample of using exclusive locks on files for simple inter-process coordiantion mechanism. The only thing that needs to be available between the processes is a shared location for file access.