mkalashy
mkalashy

Reputation: 113

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

Upvotes: 7

Views: 4966

Answers (4)

Bob Vale
Bob Vale

Reputation: 18474

Take a look at this solution for WPF applications, it makes use of a Mutex to establish uniqueness but also uses WCF to allow a new instance to communicate with the primary instance.

Upvotes: 0

stuartd
stuartd

Reputation: 73243

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.

Upvotes: 4

RBarryYoung
RBarryYoung

Reputation: 56715

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.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190897

I don't think other, non-admin users, can see the processes of the other users.

What I propose is writing a windows service that runs in the system space. Your program can then connect to it and determine whether to launch or not.

Upvotes: 0

Related Questions