Environment: C#, IDE: Visual Studio 2017
I am looking to know if xyz.exe application is running in computer under the current logged-in user.
For Example:
There are 2 users in a computer - User: A (Admin user) & User: B (Non-Admin user)
User A is logged-in the computer and run xyz.exe application and perform session lock operation.
User B is now logged-in in his/her account. (User A is not sign-out, still he/she is logged-in but his/her session is locked)
User A is having xyz.exe application running, but for user B, xyz.exe application is not running.
I want to know if xyz.exe application is running for User B. Is it possible to do that, if yes then How?
I have also updated xyz.exe and it's path where it is running from in the window registry at location: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run So that once any user do sign-out, after sign-in xyz.exe will start running automatically (It is one of the requirement in my project).
The below code is working:
/// <summary>
/// Declaring Mutex
/// </summary>
private Mutex _mutex = null;
/// <summary>
/// To check if any other instance of xyz.exe is running
/// </summary>
private bool IsSingleInstanceRunning()
{
// To check if any other instance of xyz.exe is running for currently logged-in user
_mutex = new Mutex(true, Process.GetCurrentProcess().ProcessName + Environment.UserName, out bool isSingleInstanceRunning);
// Return true if no instance of xyz.exe is running in task manager for current logged-in user.
// Return false if multiple instances of xyz.exe is running in task manager for current logged-in user.
return isSingleInstanceRunning;
}
/// <summary>
/// Entry point of application xyz.exe
/// </summary>
public void ApplicationStartup()
{
if (!IsSingleInstanceRunning())
{
System.Environment.Exit(0);
}
// Release Mutex once application is launched
_mutex.ReleaseMutex();
// Keeping mutex object alive for not creating multiple instances for multiple users
GC.KeepAlive(_mutex);
// Logic to start application xyz.exe
}