Run script on rhino close or continuously in background

I do that whenever the rhino is started it can run a script automatically.

is there any way to do the same but when the rhino is closed??

Or, make a script run loop continuously in the background, I already tried this but the rhino is in interactive mode and it is not possible to draw anything.

The goal is to create a popup window with some buttons and some textbox, a kind of toolbar