Jump to content
LaunchBox Community Forums

AutoHotKey wait for executable to close.


thedevilsjester

Recommended Posts

Using AutoHotKey scripts in LaunchBox, I am trying to work out how to wait until the launched executable has closed (and then perform some additional task).

I have tried using "WinWaitClose", I have tried using "Process, WaitClose", and I have tried to Loop (breaking only if the Window doesn't exist), but none of these methods seem to work.  Has anyone managed to get an AutoHotKey script in LaunchBox to do this?

Link to comment
Share on other sites

Are you trying to do this as an additional application for some game and running this ahk script before the games starts?

If you are, and you have a sleep in the script long enough to wait until the process is surely running before you try to kill it, you might try something like this in your loop that checks:

Process, Exist, process.exe
if(!errorlevel)
    Process, Close, process.exe
Edited by ckp
Link to comment
Share on other sites

21 minutes ago, ckp said:

Are you trying to do this as an additional application for some game and running this ahk script before the games starts?

If you are, and you have a sleep in the script long enough to wait until the process is surely running before you try to kill it, you might try something like this in your loop that checks:


Process, Exist, process.exe
if(!errorlevel)
    Process, Close, process.exe

I am not sure what you mean.

What I am trying to do is to have the AutoHotKey script that is associated with an Emulator in LaunchBox, perform an action once the Emulator that it launches exits.

Link to comment
Share on other sites

ok. same idea. you just run the process kill (or whatever task you want to do) after you exit the game with controller automation.

so here is an example of how to close xpadder when exiting the game with your controller:

you just run what you want before the the emulator exit (process, close, {{{startupexe}}}) line runs:

 

$Esc::
{
    Run, C:\Xpadder\Xpadder.exe /C
    Process, Close, {{{StartupEXE}}}
}
 

Edited by ckp
Link to comment
Share on other sites

10 hours ago, ckp said:

so here is an example of how to close xpadder when exiting the game with your controller:

The problem with this method is that it requires you to enforce your own exit button.  There are too many unknowns for this to be safe.

1. What does "Process, Close" do?  Technically.  How does each emulator respond to this?  This is very important because each application has to respond to various shut down methods in their own manner. 

Two decades of development experience tell me that thats a big can of unknown, even Microsoft says :

Quote

there is no guaranteed "clean" way to shut down an application in Win32,

Quote

This does not, however, mean that the process itself has had the opportunity to do any final flushes of information to disk

There is no reason to suspect that a Win64 application would be any different since its all based on how the developer chooses to respond to various events and what events are used to try and stop the process.

2. Most emulators have their own exit mechanism.  This guarantees a graceful exit, such as writing out SRM files, and guarantees that the emulator is in a clean shutdown state (ex: its not in the middle of writing save files).

3. If the button you use in your mapping to exit is the same button that the emulator is using for this purpose, there is a race condition.  If the emulator happens to poll and process the button first, what happens?  Will the AutoHotKey event still trigger?

To be clear, I can do a "WinWaitActive, <Title>" and it works, but "WinWaitClose, <Title>" does not.

 

Edited by thedevilsjester
Link to comment
Share on other sites

58 minutes ago, ckp said:

Just giving you options that work, at least for me. Since you have 20 years of development experience, I'm sure you'll find a way to run a script or other program the way you want.

I appreciate the responses, I was just explaining why I would not use an option like that, not that I was unappreciative of it.

I posted here because the options that should work, are not working.  I suspect that LaunchBox is killing the AutoHotKey script when the process ends so it never gets a chance to trigger those events, but until I dig deeper, that is pure speculation.  If someone else has managed to get this to work, or knows the reason that it doesn't, that would be very helpful.

Link to comment
Share on other sites

4 hours ago, jayjay said:

As @ckp says posting a script would help but yeah LB kills the script on game/emu exit.  

There is nothing to post, I simply want to press a certain button combination when the emulator exits.  Something like:

WinWaitClose, RetroArch
Send !{Space}
4 hours ago, ckp said:

you can be tricky and have LB run something that runs the script totally outside of LB. 

Its not as easy as it sounds.  Not without additional 3rd party tools.

4 hours ago, jayjay said:

What would be the problem with running a script outside LB so the script can finish before its killed by LB?

I do not wish to install additional applications other than LaunchBox/BigBox and the needed emulators.

What would be the chances that I could get them to change the behavior to not immediately kill the AutoHotKey script, just wait for the script to finish naturally (with possibly a timeout if they are worried about never ending scripts)?

Edited by thedevilsjester
Link to comment
Share on other sites

11 minutes ago, thedevilsjester said:
4 hours ago, ckp said:

you can be tricky and have LB run something that runs the script totally outside of LB. 

Its not as easy as it sounds.  Not without additional 3rd party tools.

no, i don't think that's true. i can think of a couple ways that might work. you could have ahk run some cmd or batch file that in turn runs a start command of anything you want. that will not be terminated by LB

Link to comment
Share on other sites

12 minutes ago, thedevilsjester said:

What would be the chances that I could get them to change the behavior to not immediately kill the AutoHotKey script, just wait for the script to finish naturally (with possibly a timeout if they are worried about never ending scripts)?

I think that is a great idea actually. If there was a checkbox type thing on the ahk tab for that as an option, it would be nice. 

Link to comment
Share on other sites

1 hour ago, ckp said:

no, i don't think that's true. i can think of a couple ways that might work. you could have ahk run some cmd or batch file that in turn runs a start command of anything you want. that will not be terminated by LB

It is easy to create a batch file that will execute a program, wait on the program to exit (which causes the program to spawn a new terminal window), and then use Javascript to execute the SendKeys method via Windows Script Hosts. That will "get the job done" but the end result is not something that I would be happy with.

The whole reason that I bought LaunchBox was because it was a great, integrated, all-in-one solution and it would be such a shame to stop so close to the finish line.

1 hour ago, ckp said:

I think that is a great idea actually. If there was a checkbox type thing on the ahk tab for that as an option, it would be nice. 

That would be perfect, yeah.  I will see about filing a feature request/bug report? and hope that they feel the same way.

Edited by thedevilsjester
  • Haha 1
Link to comment
Share on other sites

Im sure this will work, you will have to try, not sure its what your after but anyway...

 

As an example...

Make a new txt file and name it "my script.ahk". (do your stuff inside this file, use right click and edit to edit the script)

In LB directory, go to Launchbox\Autohotkey. 

COPY Autohotkey.exe and rename it to "my script.exe".

Place both "my script.ahk" and "my script.exe" in Launchbox\Autohotkey directory.

Now in LB go to emulator ahk script tab and write:

SetWorkingDir %A_ScriptDir%
run "my script.exe"

This will run the script outside LB without the need to install 3rd party stuff, making it portable. But remember LB wont kill the script so you will have to script that in your self.

Edited by jayjay
  • Like 2
Link to comment
Share on other sites

8 hours ago, jayjay said:

Now in LB go to emulator ahk script tab and write:

I don't know much about the script itself but @jayjayis saying that the code will only execute if placed in the emulator ahk script tab so as long as you don't enter for every one of your emulators it will only run from the specific ones you enter it in.

Link to comment
Share on other sites

3 hours ago, DOS76 said:

I don't know much about the script itself but @jayjayis saying that the code will only execute if placed in the emulator ahk script tab so as long as you don't enter for every one of your emulators it will only run from the specific ones you enter it in.

Yeah, I misread, I did not absorb the big capital word "COPY" and somehow thought that the original was being renamed.  Regardless I am not yet at the point of desperation where I want to resort to hacks to solve the problem. I am holding out hope that there is a solution that will work with LaunchBox or that they will fix the bug/add the feature once I submit the request.

Edited by thedevilsjester
Link to comment
Share on other sites

You could do this via the additional apps function by adding an .ahk file as the app and then checking the box to "automatically run after main application". This will make whatever program/script/thing you specify run once the game has closed. You can't currently edit additional apps en masse though (although this is on the to-do list) so this would only be on a per-game basis.

The problem that you're running into here is that AHK within LB ends as soon as the program that was started/launched ends. It's not like a normal external AHK script which only ends when you tell it to. In other words, it's not persistent once the launched program has ended, so it can't do anything further (like use a WinWaitClose function) at that point. You'd have to make your own (external) script that would do what you wanted, like launching a specific emulator etc. and include a WinWaitClose section to do what you want once the application closes, and then set that script as the "emulator".

I think a per-emulator-entry "automatically launch before/after" function would be nice, but it hasn't happened yet (and I wouldn't expect something this niche to be particularly high priority, honestly).

That said, @jayjay's answer should work as well, and would likely be the easiest solution.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...