Batch Stop Program A on Launch Program B, Start Program A on End Program B










0














I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.



This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).



EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.



@echo off
rem mutually exclusive program enforcer

rem initialize variables
set gDriveOn=init
set reaperOn=init

rem begin infinite loop for monitoring on states
:loopStart

tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)

rem else if reaper not found
set reaperOn="FALSE"

:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)

rem else if Google Drive not found
set gDriveOn="FALSE"

:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)

if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)

rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)









share|improve this question























  • I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
    – Jack White
    Nov 14 '18 at 21:08










  • There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
    – Zediiiii
    Nov 15 '18 at 22:28










  • You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
    – Jack White
    Nov 17 '18 at 8:00










  • I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
    – Zediiiii
    Nov 18 '18 at 9:13















0














I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.



This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).



EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.



@echo off
rem mutually exclusive program enforcer

rem initialize variables
set gDriveOn=init
set reaperOn=init

rem begin infinite loop for monitoring on states
:loopStart

tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)

rem else if reaper not found
set reaperOn="FALSE"

:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)

rem else if Google Drive not found
set gDriveOn="FALSE"

:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)

if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)

rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)









share|improve this question























  • I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
    – Jack White
    Nov 14 '18 at 21:08










  • There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
    – Zediiiii
    Nov 15 '18 at 22:28










  • You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
    – Jack White
    Nov 17 '18 at 8:00










  • I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
    – Zediiiii
    Nov 18 '18 at 9:13













0












0








0







I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.



This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).



EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.



@echo off
rem mutually exclusive program enforcer

rem initialize variables
set gDriveOn=init
set reaperOn=init

rem begin infinite loop for monitoring on states
:loopStart

tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)

rem else if reaper not found
set reaperOn="FALSE"

:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)

rem else if Google Drive not found
set gDriveOn="FALSE"

:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)

if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)

rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)









share|improve this question















I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.



This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).



EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.



@echo off
rem mutually exclusive program enforcer

rem initialize variables
set gDriveOn=init
set reaperOn=init

rem begin infinite loop for monitoring on states
:loopStart

tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)

rem else if reaper not found
set reaperOn="FALSE"

:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)

rem else if Google Drive not found
set gDriveOn="FALSE"

:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)

if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)

rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)






batch-file batch-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 9:52

























asked Nov 12 '18 at 0:34









Zediiiii

368313




368313











  • I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
    – Jack White
    Nov 14 '18 at 21:08










  • There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
    – Zediiiii
    Nov 15 '18 at 22:28










  • You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
    – Jack White
    Nov 17 '18 at 8:00










  • I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
    – Zediiiii
    Nov 18 '18 at 9:13
















  • I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
    – Jack White
    Nov 14 '18 at 21:08










  • There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
    – Zediiiii
    Nov 15 '18 at 22:28










  • You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
    – Jack White
    Nov 17 '18 at 8:00










  • I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
    – Zediiiii
    Nov 18 '18 at 9:13















I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
– Jack White
Nov 14 '18 at 21:08




I cannot find doMoreStuff: line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
– Jack White
Nov 14 '18 at 21:08












There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28




There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28












You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
– Jack White
Nov 17 '18 at 8:00




You have at least one syntax error: domorestuff needs to start with :. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
– Jack White
Nov 17 '18 at 8:00












I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13




I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13












1 Answer
1






active

oldest

votes


















1














Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.



As for performance:



Batch file delays



if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.



This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.



You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:



TIMEOUT /T 5 /NOBREAK


where 5 is seconds to wait. See timeout /? or this for more information.



AutoIT



In AutoIt you will need ProcessExists() to check process existence and Sleep() to insert a delay. Killing a process is accomplished with ProcessClose() and Run() starts one. The documentation is available here under Function Reference



Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250) or more.



However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe") you could do something like this:



local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend


This works because ProcessExists() returns PID if process exists or 0 if not.
First if line runs if $pid was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else because 1st check may set $pid to 0.



EXE replacement



If the solutions above do not work -- for example because the reaper.exe just crashes horribly if it happens to attempt accessing something googledrivesync.exe is holding -- then something else should probably be done.



One option is to rename reaper.exe to real_reaper.exe and compile an autoit script into new reaper.exe that kills googledrivesync and starts real_reaper.exe. This script needs to pass all parameters so something like



RunWait ('"real_reaper.exe" ' & $CmdLineRaw)


should do the trick. This waits for real_reaper.exe to finish running and then you can do Run("googledrivesync.exe")



This may not work properly if the program that starts reaper.exe uses some tricks but it should work in most cases.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254648%2fbatch-stop-program-a-on-launch-program-b-start-program-a-on-end-program-b%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.



    As for performance:



    Batch file delays



    if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.



    This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.



    You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:



    TIMEOUT /T 5 /NOBREAK


    where 5 is seconds to wait. See timeout /? or this for more information.



    AutoIT



    In AutoIt you will need ProcessExists() to check process existence and Sleep() to insert a delay. Killing a process is accomplished with ProcessClose() and Run() starts one. The documentation is available here under Function Reference



    Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250) or more.



    However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe") you could do something like this:



    local $pid = 0
    while true
    if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
    if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
    if ($pid <> 0) then
    ;do something if process exists...
    endif
    sleep(300)
    .........
    wend


    This works because ProcessExists() returns PID if process exists or 0 if not.
    First if line runs if $pid was already known from last time we checked.
    If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else because 1st check may set $pid to 0.



    EXE replacement



    If the solutions above do not work -- for example because the reaper.exe just crashes horribly if it happens to attempt accessing something googledrivesync.exe is holding -- then something else should probably be done.



    One option is to rename reaper.exe to real_reaper.exe and compile an autoit script into new reaper.exe that kills googledrivesync and starts real_reaper.exe. This script needs to pass all parameters so something like



    RunWait ('"real_reaper.exe" ' & $CmdLineRaw)


    should do the trick. This waits for real_reaper.exe to finish running and then you can do Run("googledrivesync.exe")



    This may not work properly if the program that starts reaper.exe uses some tricks but it should work in most cases.






    share|improve this answer



























      1














      Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.



      As for performance:



      Batch file delays



      if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.



      This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.



      You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:



      TIMEOUT /T 5 /NOBREAK


      where 5 is seconds to wait. See timeout /? or this for more information.



      AutoIT



      In AutoIt you will need ProcessExists() to check process existence and Sleep() to insert a delay. Killing a process is accomplished with ProcessClose() and Run() starts one. The documentation is available here under Function Reference



      Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250) or more.



      However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe") you could do something like this:



      local $pid = 0
      while true
      if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
      if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
      if ($pid <> 0) then
      ;do something if process exists...
      endif
      sleep(300)
      .........
      wend


      This works because ProcessExists() returns PID if process exists or 0 if not.
      First if line runs if $pid was already known from last time we checked.
      If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else because 1st check may set $pid to 0.



      EXE replacement



      If the solutions above do not work -- for example because the reaper.exe just crashes horribly if it happens to attempt accessing something googledrivesync.exe is holding -- then something else should probably be done.



      One option is to rename reaper.exe to real_reaper.exe and compile an autoit script into new reaper.exe that kills googledrivesync and starts real_reaper.exe. This script needs to pass all parameters so something like



      RunWait ('"real_reaper.exe" ' & $CmdLineRaw)


      should do the trick. This waits for real_reaper.exe to finish running and then you can do Run("googledrivesync.exe")



      This may not work properly if the program that starts reaper.exe uses some tricks but it should work in most cases.






      share|improve this answer

























        1












        1








        1






        Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.



        As for performance:



        Batch file delays



        if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.



        This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.



        You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:



        TIMEOUT /T 5 /NOBREAK


        where 5 is seconds to wait. See timeout /? or this for more information.



        AutoIT



        In AutoIt you will need ProcessExists() to check process existence and Sleep() to insert a delay. Killing a process is accomplished with ProcessClose() and Run() starts one. The documentation is available here under Function Reference



        Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250) or more.



        However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe") you could do something like this:



        local $pid = 0
        while true
        if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
        if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
        if ($pid <> 0) then
        ;do something if process exists...
        endif
        sleep(300)
        .........
        wend


        This works because ProcessExists() returns PID if process exists or 0 if not.
        First if line runs if $pid was already known from last time we checked.
        If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else because 1st check may set $pid to 0.



        EXE replacement



        If the solutions above do not work -- for example because the reaper.exe just crashes horribly if it happens to attempt accessing something googledrivesync.exe is holding -- then something else should probably be done.



        One option is to rename reaper.exe to real_reaper.exe and compile an autoit script into new reaper.exe that kills googledrivesync and starts real_reaper.exe. This script needs to pass all parameters so something like



        RunWait ('"real_reaper.exe" ' & $CmdLineRaw)


        should do the trick. This waits for real_reaper.exe to finish running and then you can do Run("googledrivesync.exe")



        This may not work properly if the program that starts reaper.exe uses some tricks but it should work in most cases.






        share|improve this answer














        Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.



        As for performance:



        Batch file delays



        if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.



        This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.



        You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:



        TIMEOUT /T 5 /NOBREAK


        where 5 is seconds to wait. See timeout /? or this for more information.



        AutoIT



        In AutoIt you will need ProcessExists() to check process existence and Sleep() to insert a delay. Killing a process is accomplished with ProcessClose() and Run() starts one. The documentation is available here under Function Reference



        Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250) or more.



        However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe") you could do something like this:



        local $pid = 0
        while true
        if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
        if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
        if ($pid <> 0) then
        ;do something if process exists...
        endif
        sleep(300)
        .........
        wend


        This works because ProcessExists() returns PID if process exists or 0 if not.
        First if line runs if $pid was already known from last time we checked.
        If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else because 1st check may set $pid to 0.



        EXE replacement



        If the solutions above do not work -- for example because the reaper.exe just crashes horribly if it happens to attempt accessing something googledrivesync.exe is holding -- then something else should probably be done.



        One option is to rename reaper.exe to real_reaper.exe and compile an autoit script into new reaper.exe that kills googledrivesync and starts real_reaper.exe. This script needs to pass all parameters so something like



        RunWait ('"real_reaper.exe" ' & $CmdLineRaw)


        should do the trick. This waits for real_reaper.exe to finish running and then you can do Run("googledrivesync.exe")



        This may not work properly if the program that starts reaper.exe uses some tricks but it should work in most cases.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 13:19

























        answered Nov 17 '18 at 9:56









        Jack White

        33416




        33416



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254648%2fbatch-stop-program-a-on-launch-program-b-start-program-a-on-end-program-b%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20