Check if the application is running

I am developing an application in cocoa that needs to check if this application is running at startup. If it is already running, I need to exit the new instance. Is there any way to do this. for the perfect solution

+4
source share
3 answers

It sounds like you're saying you want to run multiple instances of your cocoa application at the same time. Typically, cocoa applications do not allow you to run multiple instances at the same time, so you will not need to perform this check. Is there any particular circumstance in which you find that the cocoa application is starting at the same time?

All in all, cocoa's way to solve this look at running applications in NSWorkspace. This returns an NSArray containing a dictionary for each running application. You can go through the array to find out if the application you are looking for is working. I would advise using the value with the NSApplicationBundleIdentifier key, which will have a value, for example, "com.mycompany.myapp", and not look for the name. If you need to find the package identifier for the application, you can look at its info.plist file in the application package.

+5
source

My answer here is not specific to the implementation of object-c, but as a general approach. On systems like * nix, the daemon usually creates a pid file somewhere to indicate its existence. If the daemon does not allow multiple instances, then another application fire must first check if such a pid file exists, if it exists, itself.

0
source

you can open () an instance of the ps command and find the name of the application. if you find it, turn off the new one. maybe not the fastest way, but it works :-)

0
source

All Articles