且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测 OS X 应用程序是否已启动

更新时间:2023-01-25 21:57:28

一个低级的解决方案是使用 flock().

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

每个实例都会在启动时尝试锁定一个文件,如果锁定失败,则另一个实例已经在运行.当你的程序退出时,Flocks 会自动释放,所以不用担心过时的锁.

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?