如何检测的OS X应用程序是否已经启动 [英] How to detect whether an OS X application is already launched

查看:123
本文介绍了如何检测的OS X应用程序是否已经启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在OS X应用程序绑定只能使用一次,但是通过简单的复制束相同的应用程序可以启动两次启动。什么是及时发现并制止这种可能性的最佳策略是什么?

Normally an application bundle on OS X can only be started once, however by simply copying the bundle the same application can be launched twice. What's the best strategy to detect and stop this possibility?

在视窗这种效果可以简单地通过应用程序创建在发射一个命名的资源,然后,如果无法创建指定的资源出口来实现,指示另一进程正在运行一个已经创建的相同的资源。当程序退出这些资源被释放,以可靠的方式在Windows上。

On Windows this effect can simply be achieved by the application creating a named resource at launch and then exit if the named resource can't be created, indicating that another process is running that has already created the same resource. These resources are released in a reliable way on Windows when the application quits.

研究这是在OS X上的API保持状态,在文件系统中,因此,当我看到这个问题使得在Windows上不可靠的,即不正确的退出后挥之不去的文件中使用可以错误地指出应用程序已经运行策略

The problem I have seen when researching this is that the APIs on OS X keep state in the file system and thus makes the strategy used on windows unreliable, i.e lingering files after an improper exit can falsely indicate that the application is already running.

我可以用它来实现在OS X上的效果相同的API有:POSIX,碳和提升

The APIs I can use to achieve the same effect on OS X are: posix, carbon and boost.

想法?

推荐答案

一个低级别的解决方案是使用羊群()。

A low-level solution is to use flock().

每个实例将试图锁定在启动时的文件,并且如果锁定失败,则另一实例已在运行。鸡群会被自动释放当你的程序退出,所以没有对陈旧的锁后顾之忧。

Each instance would attempt to lock a file on startup, and if the lock fails then another instance is already running. Flocks are automagically released when your program exits, so no worries about stale locks.

请注意,您选择的任何解决方案,你需要做意味着什么有多个实例一个明智的决定。特别是,如果多个用户在同一时间运行你的应用程序,可以吗?

Note that whatever solution you choose, you need to make a conscious decision about what it means to have "multiple instances". Specifically, if multiple users are running your app at the same time, is that ok?

这篇关于如何检测的OS X应用程序是否已经启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆