Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One instance application over multiple Windows user accounts

I have used solution of Mutex to disallow opening more than one instance of my application at the same time

but, when i use Switch User in windows and open another user, and try to open the application, it opened normally and can't detect that it's running on other account

how could i solve something like this and disallow opening the application among Logged in Windows User Accounts

i am using code like this

Dim onlyInstance As Boolean = False
_mutex = New Mutex(True, "Application Name", onlyInstance)
If Not onlyInstance Then
   MessageBox.Show("Application is already running.", "Error.")
   System.Diagnostics.Process.GetCurrentProcess.Kill()
   System.Diagnostics.Process.GetCurrentProcess.WaitForExit()
End If
like image 630
mkalashy Avatar asked Oct 15 '12 15:10

mkalashy


2 Answers

If you make your mutex global, then it will be visible to all users:

mutexName = String.Format("Global\\{{{0}}}", "Application Name"); 

From the documentation:

A named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

like image 65
stuartd Avatar answered Oct 11 '22 22:10

stuartd


The technique that I have used for 30 years is to simply have the App try to open (and hold open) a known file for exclusive access when it starts up, and then to exit if it cannot open that file.

I know it's seriously low-tech and retro, but it's simple, always supported and works literally everywhere. I have used it successfully in production code on over half a dozen OS's and in more than a dozen different programming languages.

like image 45
RBarryYoung Avatar answered Oct 12 '22 00:10

RBarryYoung