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.
Change the Remote Desktop Connection port on Windows Server
This manual apply to Windows Server 2012, 2008 R2, 2008, 2003
The latest attacks of ransomware viruses exploit poorly protected Remote Desktop access to connect to Windows servers, then crack weak passwords and encrypt sensitive data. Hundreds of hackers continously scan the Internet to find poorly protected servers. Once you install Windows Server, it gets to the scope of hackers rather quickly - in a couple of weeks of so.
Of course, the first countermeasure to avoid accidental hacker attack is to use secure password. Never use simple or easily guessed passwords, use at least 20 character passwords with different letter casing, numbers, special symbols.
One additional countermeasure to fight automated hacker attacks is to move RDP port from default 3389 to different number, which is known only to you. This will get your server off automated port scann alorithms used by hackers and viruses.
As we said before, default Windows servers' Remote Desktop is listening on TCP port 3389. You can change the port to different number in range 1024-65535.
Do not use lower port number since it is reserved by system.
IMPORTANT: Make sure that remote access to your server through the new port is authorized in your Windows firewall before executing the next steps.
How to get the current executable's path in C# (Code sample)
Sometimes you may want to get the current executable file path of your C# program. The working folder is needed to access settings, database, images, or resource files residing in the same directory as the currently running C# executable file of your program.
There are several options to find the current executable path in C#. But we have found the one that is working both for C# .NET Console Applications and for C# Windows Forms Applications: