限制应用程序的多个实例
好的,我已经创建了我的 C# 应用程序,为其创建了一个安装程序,并将其安装在我的计算机上。
问题是,当用户打开应用程序exe两次时,将会有两个应用程序实例在运行。 我只希望应用程序的一个实例在任何时候运行,我该如何做到这一点?
感谢您的帮助,
Okay, so i've created my c# application, created an installer for it and have it working installed on my machine.
The problem is, when the user opens the application exe twice, there will be two instances of the application running. I only ever want one instance of the application to be running at any time, how do I go about doing this?
Thanks for your help,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常见的技术是创建一个命名的互斥体并在应用程序启动时检查它是否存在。
请参阅此或这个。
来自 DDJ 的代码:
The common technique for this is to create a named
Mutex
and check for its presence on application start.See this or this.
Code from DDJ:
我通过这个解决了这个问题,
这很简单,但我几乎没有时间检查更好的解决方案。
i solved this problem by this
it is simple, but i had hardly time to check for better solutions.
感谢先生们。 艾伦和鲍威尔:
With thanks to Messrs. Allen and Powell:
我不知道您正在运行的环境,但关于“单实例应用程序”需要记住的是如何定义单实例。 如果应用程序可以使用通用数据源同时在多个工作站上运行,这会是一个问题吗? 同样,对于多个用户登录同一台计算机的终端服务情况(或“运行方式”情况),您是否希望以每个用户、每个用户只能运行一个实例的方式限制应用程序? -电脑? 或者您是否同意每个用户只有一个实例?
这些问题的答案可能会引导您走向一个方向而不是另一个方向。 例如,我们有一个“单实例”应用程序,其范围是一组计算机。 该组工作站中只允许一个用户使用。 我们通过在共享数据源中创建一个表来跟踪当前连接的用户来管理此操作。 这是一个维护问题,因为您需要确保该表始终 100% 准确。 处理诸如工作站意外断电、在该表中留下“虚假”记录之类的事情需要谨慎处理。
I don't know the environment that you are operating in, but something to keep in mind about 'single-instance applications' is how you define single-instance. If the application can be run on multiple workstations at the same time, using a common datasource, is that an issue? Likewise, what about a terminal-services situation (or a "run as" situation) where more than one user is logged into the same computer, do you want to restrict the application in such a way that only one instance per-user, per-computer? Or are you okay with it simply being one instance per user?
The answer to these might lead you in one direction over another. For example, we have a 'single-instance' application with the scope being a group of computers. Only one user is allowed on within that group of workstations. We managed this by have a table in our shared data-source that tracked currently connected users. This is a maintenance issue as you need to be sure that table is 100% accurate all the time. Handling things like unexpected power outages on the workstation, leaving "bogus" records in that table took some careful handling.