There was a requirement that the application can't run more than one instance, and if already running then show
the message to the user and set that window as active.
For this purpose we need System.Threading.Mutex, which synchronizes the execution of threads across process boundaries.
Also, we can use SetForegroundWindow function which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user.
Here is a small example code:
static class Program
indicate application has initial ownership of mutex
private static bool
private static extern bool
/// The main entry point for the application.
static void Main()
mutex at application start and try to get the ownership
(var m = new Mutex(true, "OnlyOneInstanceOfApp", out isNew))
application owns the mutex, continue the execution
show user message that application is running and set focus to that application
MessageBox.Show("Application already running");
current = Process.GetCurrentProcess();
(Process process in Process.GetProcessesByName(current.ProcessName))
if (process.Id != current.Id)
When we run the application we see
And now if we again run the application we see the message: