tags:

views:

68

answers:

3

The situation is that I want to allow users to open multiple instances of the program but I do not want more than one logged on user to use the program at once. So, for instance if the program is installed on a server and multiple users can Remote Desktop to the server I only want one user to be able to run the program but still be able to run multiple instances. I have looked into using a mutex but from what I've found a mutex would only allow one instance of the program. Is this something that is possible with C#?

A: 

I would disable the built-in one instance feature and write your own. Just step through all running processes, and if you find yours, see if the user equal to the current user. If it is, exit one of the instances.

Edit: This would require users to have admin rights, which isn't likely in the situation you propose. There is probably a better way to do this, but you could always do some locking in a file or registry... some area that all users have read/write access to. Update that lock file with the current username and a timestamp every x seconds or so. If another user tries to open the program, then you can tell them who has it open. If the current user already has it open, no big deal. If the lock file is out of date, ignore it... you can assume the instance that created it is no longer there.

Brad
This would require that all users that log on to the remote server have administrator privileges. Otherwise they are not able to list processes running under different credentials.
klausbyskov
Ah, good point! Editing my answer...
Brad
+3  A: 

This is something typically handled via a separate application which runs as a license manager. You can have a service that serves licenses, and tracks current usage, and make your application connect to the manager at startup. The manager can check the credentials, and respond whether to allow the application to run or not depending on any criteria - including only 1 user (with multiple copies), up to N users, etc.

Windows Communication Foundation works well for implementing the communication to/from the service.

Reed Copsey
Unfortunately I don't have the option of having our users installing a separate application in order to run this one.
Alex Torres
@Alex: Unfortunately, without elevating permissions, there's no real effective way to control what a different user does (by design). You can limit the number of applications within one user's login, but not across a machine without either admin rights or a moderating service.
Reed Copsey
+2  A: 

I would use a named Mutex. Inside of its protection, keep a record of the current logged on user. If one exists, and it's not the current user, then exit - otherwise, record that the current user is running the application.

Ben
Thanks for the suggestion. I ended up using a system of two named mutexes and it seems to be working out pretty well. Now I'll have to see if QA is able to break it.
Alex Torres