开发者

How to allow only one user to run my C# program, but also allow multiple instances?

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#?


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.


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.


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.


Use a named Mutex. The Mutex name will be MyMutex_[username] This way you will have a different single Mutex for each user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜