2

I understand how to use Mutex to limit an application to run only a single instance at one time. However, in my application I allow multiple instances to run simultaneously, but I need a way to count and/or list all of the running instances for a given user, regardless of whether or not the executable file has been renamed. Can Mutex be used here or is the process class able to do this? I know how to use the process class to list processes by name, but what if the exe has been renamed by the user? How is this best handled?

EDIT: For my purposes it turned out to be sufficient to merely be able to count other instances of the same application rather than list them out. To count them I used a named semaphore.

//Initialize the semaphore with an initial value of 10000 and a maximum value of 10000.  The 'Global\' prefix will be system wide.  Remove it to limit the semaphore to the current logon session
Semaphore mySemaphore = new Semaphore(10000, 10000, @"Global\mySemaphoreUniqueNameGoesHere");
//Call .WaitOne() to enter the semaphore and decrement the semaphore count
mySemaphore.WaitOne();

Since each running instance of the application will decrement the count by 1, then any time I need to check how many instances of the application are running I can use:

//.Release() will exit the semaphore and increment the semaphore count, but it will also return the value of the count just before .Release() was called, which is what we really need here.
int numberOfRunningInstances = 10000 - mySemaphore.Release();
//Then we can simply use .WaitOne() to re-enter the semaphore and decrement the count back to what it was before calling .Release()
mySemaphore.WaitOne();

Then when the application exits:

//call .Release() to exit the semaphore and increment the count
mySemaphore.Release()
blitz_jones
  • 1,048
  • 2
  • 10
  • 22
  • To clarify: If I have 1.exe, 2.exe, and 3.exe, but they are all identical copies of the same exe only they have been renamed differently, how can I get a list of these running instances of the same application when they are not named the same? – blitz_jones Mar 28 '17 at 21:01
  • 1
    You could use shared memory (in Windows terminology, a file mapping, though there doesn't need to be an actual file involved) and have each process add their own process ID to the list. – Harry Johnston Mar 28 '17 at 22:11

1 Answers1

1

This is not an exact duplicate but very close to this existing question about listing all processes for all users.

From the management object you can invoke the GetOwner method like this :

 string[] o = new String[2];
 queryObj.InvokeMethod("GetOwner", (object[])o);

EDIT:
Several possible solutions, depending on what you want to achieve and what kind of complexity you can handle / need.

1- You can retrieve the path of each running binary, calculate a checksum for the executable file and compare it to the others. This might not be the solution you are looking for.

2- If you control the source for those applications, you could have them register something system wide at startup. It seems to be possible to register global semaphores and then retrieve the current count.

3- Implement some kind of shared memory between all instances, with memory mapped files for example and share all interesting information between them. This brings more complications, like cleaning up that memory properly when your process exits and probably locking as well.

4- You can implement some kind of system wide event broadcaster / receiver through named events. When the process starts, it writes its pid in a new file in a hardcoded folder. When your process is notified of a new startup, it reads every file in the folder and gets the pids.

Community
  • 1
  • 1
Eric
  • 19,525
  • 19
  • 84
  • 147
  • I think this is missing the primary purpose of my question. The issue is not how to get the owner of the process. The issue is how to get the processes that are the same executable even if the .exe has been renamed. So if I have 1.exe, 2.exe, and 3.exe, and they are all copies of the exact same .exe but renamed, how can I get a list of these? – blitz_jones Mar 28 '17 at 20:56
  • Sorry for missing that. Can you explain which cases you want to cover? executable files are usually locked for modifications when they are in use. How can they be renamed? Like with copies? – Eric Mar 28 '17 at 21:11
  • They are not renamed during use. I'm just talking about the situation where one exe file is copied several times, and each copy has a different name. Then they are launched after they have been copied/renamed. Thanks. – blitz_jones Mar 28 '17 at 21:15
  • executable files easy can be renamed while exe is running - it can not be deleted or modified. but renamed can – RbMm Mar 28 '17 at 21:33