How to automatically close App_A when I close App_B using batchfile

帅比萌擦擦* 提交于 2019-12-24 19:16:13

问题


Hi everyone I'm a newbie in batchfiling but loved tinkering and coding every other time. Can I make a batch file that closes two program simultaneously? Example, I created a batchfile that opens two program, App_A (gamepad imulator) is minimized App_B (offline RPG Game) normal window. What I want is when I close App_B App_A would automatically close too so that I don't have to restore the window and manually close the imulator.

this is the code I just did which I also found in this site and got it working:

ECHO OFF
start /d "C:\Documents and Settings\Computer\My Documents\Imulator\PROFILE" SET1.imulatorprofile /m
ECHO IMULATOR STARTED
start /d "C:\Program Files\App_B" App_BLauncher.exe
ECHO APP_B STARTED
ECHO OPERATION COMPLETE

Any comments or suggestions is GREATLY APPRECIATED! THANKS in ADVANCE :)


回答1:


You can use another batch file with two taskkill lines, one for each of your apps, and launch that.

Otherwise you'd need to have a batch file running all the time in a window, which loops and checks if appB is not running and then it will close appA. It's not very elegant.




回答2:


I'm not very good using the windows commandline, but I would try the following approach:

  1. start imulator (which should quit automatically after APP_B exited)
  2. start APP_B (using the /wait option - this should pause the batch processing)
  3. kill imulator (using PsKill by name)

You can find details about start, PsKill and other commands at this site.

Hope that helps a bit.

*Jost


...added later...

Another option would be to do regular checks in the background if a process (App_B) is running and continue (with stopping App_A) when it is finished. This approach makes sense when App_B is only a launcher for another process (e.g. App_Launched_By_B) and comes back directly.

This can be done with a small loop which might look similar to this one:

start "App_A" /d "C:\Programs\App_A" App_A.exe
ECHO App_A STARTED
start "App_B" /d "C:\Programs\App_B" App_B.exe
ECHO App_B STARTED

ECHO GIVE App_B 30 SECONDS TO LAUNCH App_Launched_By_B 
SLEEP 30

:LOOP
PSLIST App_Launched_By_B >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  ECHO App_Launched_By_B IS STILL RUNNING - PAUSE ANOTHER 5 SECS
  SLEEP 5
  GOTO LOOP
)

:CONTINUE
PsKill App_A
ECHO App_A STOPPED

This example came originally from another answer and was written by aphoria. I adapted it just a little little bit for this case.

Additional information about PsList and other commands can be found at this site.

I also want to note that I do not really like this approach, because it consumes some cpu without doing much. But it is the only solution from a batch file I can see.



来源:https://stackoverflow.com/questions/17670793/how-to-automatically-close-app-a-when-i-close-app-b-using-batchfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!