Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

cocoa - Ideal way to single-instance apps on the Mac

On Windows, it's common practice to create a named mutex and use the presence of that to determine that an instance of a given app is already running. This has its drawbacks, but mostly works.

I can think of a ways to do this on the Mac:

  1. named pthread mutexes
  2. enumerate running processes and look for one that matches
  3. create and lock a file

Is there something built into Cocoa/Carbon that's easier than the options above? If not, which of the three are most used on the mac? I would assume 2 or 3...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To elaborate further on using NSWorkspace. Try using launchedApplications in NSWorkspace. This returns an NSArray containing a dictionary for each launched application. You can loop through the array to see if the app you are looking for is already running. I would advise that you use the value with the key NSApplicationBundleIdentifier which will have a value like "com.mycompany.myapp" rather than looking for the name. If you need to find the bundle identifier for an app you can look at its info.plist file in the app package.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...