Using C# Mutexes for interprocess synchronization
Mutexes are used in multithreaded (multiprocess) environment for organizing a mutually exclusive access to some shared resource. While usually not required for simple applications, these syncronization primitives are crucial once you advance to mutli-threaded world. Consider you have some resource, which can only be accessed from a single process or thread for consistency. Say, you have a connection to some remote server that can process only one operation at a time. Multiple requests are dropped. But you need to write two programs that do some data processing and query the server periodically. Of course, you can run them in chain one after another. Run first program, wait until it finishes, then run second program. But what if you want your programs to do background work simultaneously and only block on server requests? This will be much more effective considering you have enough CPU power at your server. There are many other similar examples like sharing the same serial port when communicating with some peripheral, etc.
In this example we will discuss how to create and correctly use a named mutex object.