What is the right way to cleanly terminate AmiBroker from an external program? The external program will have full access to the Win32 API, so the process pid or handle will be known.
You might use command line taskkill
taskkill /im Broker.exe
Specifies the image name of the process to be terminated.
Use the wildcard character (*) to specify all image names.
Upper command line is not forced kill.
A forced kill of AB without AB doing saving process before would be
taskkill /f /im Broker.exe
@fxshrat Thank you for sharing the taskkill idea. There is also an option to terminate the process by PID:
taskkill /PID processId
No, no, no.
Killing task is definitely NOT good idea and is definitely NOT "clean".
Do NOT use taskkill. If you do your data will be lost.
The most clean way to close Windows process is to post WM_CLOSE message to main application window. It is equivalent of user pressing "Close" button in the upper right corner.
You could also use OLE Quit() from http://www.amibroker.com/guide/objects.html
Compelled to post here especially because of the OLE option
It's used so commonly in my script, yet couldn't recall it when needed out of the blue.
There is a simple MS KB article that has almost the same name as this topic and thought of posting it.
Its about WM_CLOSE message but someone definitely took time to explain clearly the internals of resource freeing up.
Modern OS do alot more in the background.
It turns out, the
taskkill utility does post a
WM_CLOSE message. I checked it with spy++. However, it might also take other less graceful actions. The OLE Quit method is probably safer.
- Your question was about Windows API. PostMessage( hWnd, WM_CLOSE, 0, 0 ) is Windows API. Taskkill is not. Taskkill may require admin rights on some installations.
- Microsoft did not document what Taskkill is doing internally so you are guessing. Even if it sends WM_CLOSE to main window it does not mean that it does not send it to other top level windows. If it does this may result in data loss. Also what if application does not respond to WM_CLOSE. Or how long do they wait? Do they call TerminateProcess()? You simply don't know, because Microsoft did not tell.
So my original answer above still applies.