How to make the system wait?

I close an application sh.Execute(“Killall myapp”) then reopen it immediately to take the changes into account.
I have an error message when reopening probably because the app does not have time to completely close and still remains in memory.
If, between the two commands, I put a loop “for i=0 to 1000000 next I”, for example, the app restarts without any problem.
My question is what command is usually used to pause the system?

App.SleepCurrentThread(Milliseconds As Integer)

It’s better to include error message(s) with your questions, this guides us in helping you.

Out of curiosity, why do you need to kill your app ?

Hopefully this was merely an example since you should never use loops to delay a program. Also, using “killall” is a heavy-handed approach which should rarely be necessary inside a program. It sounds like you need to rethink what you’re actually trying to accomplish. For example:

  • Adding a delay to ensure a clean exit would be better handled by using the Closing event.

More details would be helpful.

Yes. Sometimes when my app quits it has to wait for some communications with an HTMLViewer to complete. I do this by starting a thread which immediately starts checking, once every 100msec (done by sleeping for 100msec), to see whether those communications have completed. When they have, the thread quits the app.

I misspoke, I don’t want to kill my app, but an external Apple app.

I misspoke, I don’t want to kill my app, but an external Apple app.

Why don’t you quit it using an AppleEvent? It’ll be more safe.

1 Like

OK, but what Apple event for example ?

I just used a timer and it worked.

Var ae as new AppleEvent("aevt","quit","com.bundle.ID")
Var b as boolean=ae.send

(“com.bundle.ID” being the bundle ID of the target app).

That’s the recommended way to quit most apps.


A timer is definitely another good option - especially if you leave macOS where Apple Events don’t exist.

Thank you for the info @Arnaud_N,
I noticed that if you quit the Finder (or the Dock) with this code, it doesn’t restart itself, unlike a command like Shell.Execute(“killall Finder”) for example.
How to restart it?

The Finder indeed won’t restart automatically after the AppleEvent, but the Dock definitively should (it’s handled by launchd). Are you sure in your case the Dock doesn’t restart?

For the Finder, you can restart it by launching it again.
The recommended way is to search for its path and open it.
To locate the Finder app programatically, (1) if you have the MBS plugins, you can use this:

Var f As FolderItem=LaunchServicesFindApplicationForInfoMBS("","","")
or (2), if you don’t have the plugins, there are declares to achieve the same goal but I can’t find them right now.
Alternatively, you can hard-code the path of the Finder, since it hasn’t changed since Mac OS X has existed; that’s not recommended, as Apple may decide to change its location in the Future (whereas the methods to locate it programmatically would continue to function), but you may go with it and fix your code years later if it ever breaks:

Var f As FolderItem=SpecialFolder.System.Child("Library").Child("CoreServices").Child("")

Whichever method you choose, you can then use: