Task : Launch X after application after Y launched

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #11

    I will play with this a bit later, but here's my idea if someone wants to give it a try already:
    • Create a PowerShell script
    • Start first program:
      • Code:
        [System.Diagnostics.Process]::Start("Program_1").WaitForExit(5000)
      • Wait for Exit delay is set for 5 seconds (5,000 milliseconds); if the Program_1 is not closed within this time frame, executing the script continues. This will give the Program_1 some time to completely start before Program_2 is launched, increase the value if 5 seconds is not enough
    • Start Program_2, this time with Wait for Exit switch with no value:
      • Code:
        [System.Diagnostics.Process]::Start("Program_2").WaitForExit()
      • The Wait for Exit switch without a value causes the script to halt and wait until the Program_2 is closed
    • Now kill first task (program) with Taskkill

    Should work.

    Kari
    Last edited by Kari; 06 Aug 2013 at 15:22. Reason: Typo
      My Computer


  2. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #12

    OK, here's the PowerShell script, delay between launch of Program_1 and Program_2 set to 10 seconds (10,000 milliseconds). Use Notepad or other text editor to write the script, save it with .ps1 extension, for instance F:\Scripts\MyScript.ps1.

    The instructions might look complicated but this really is quite fast and easy.

    This script would work as follows:
    • Start Program 1
    • Wait given time, in this example 10 seconds
    • Start Program 2
    • When Program 2 is manually closed, close Program 1
    • Exit PowerShell

    1. The script:
    Code:
    [System.Diagnostics.Process]::Start("Path_to_Program_1").WaitForExit(10000)
    [System.Diagnostics.Process]::Start("Path_to_Program_2").WaitForExit()
    taskkill /IM Name_of_the_process_Program_1
    exit
    Highlighted parts:
    • Lines 1 and 2:
      Name and path of Programs 1 & 2, for instance C:\Windows\System32\notepad.exe or D:\Games\MyGame\Rally.exe
    • Line 3:
      Process (image) name for process (program) 1, for example notepad.exe. The second process needs no Taskkill as Program_2 is already manually closed for the script to continue.

    2. Change PowerShell Script Execution Policy:

    Before running the script you have to change the PowerShell Execution Policy. This must be done only once, any change in policy will be active until the next time you decide to change it. By default the policy is set to Restricted, meaning no scripts are allowed to run.

    To do this run PowerShell elevated (as administrator) and change the policy as told here:
    • Click Start
    • Type PowerShell to Start Menu Search field
    • Right click PowerShell
    • Select Run as administrator
      Task : Launch X after application after Y launched-2013-08-07_140151.png
    • Accept the UAC prompt by clicking Yes
    • Type the following command to PowerShell, press Enter:
      Set-ExecutionPolicy Unrestricted
    • Accept the policy change by typing Y, press Enter
      Task : Launch X after application after Y launched-2013-08-07_140406.png

    The policy is now changed, you can close the elevated PowerShell window and continue from below.

    3. Run the script:
    • Save the script with .ps1 extension, for instance F:\Scripts\MyScript.ps1
    • Now simply run the script using Run dialog (WIN + R) giving the following command or create a shortcut for this command:
      powershell.exe F:\Scripts\MyScript.ps1

    Notice please: If the launch of the Program_2 does not need to wait until the Program_1 has completely started, leave the Wait for Exit switch away from first line. In this case the script would be:
    Code:
    [System.Diagnostics.Process]::Start("Path_to_Program_1")
    [System.Diagnostics.Process]::Start("Path_to_Program_2").WaitForExit()
    taskkill /IM Name_of_the_process_Program_1
    exit
    That's it.

    Kari
    Last edited by Kari; 08 Aug 2013 at 04:33. Reason: Added information about enabling scripting.
      My Computer


  3. Posts : 289
    Win7x64
    Thread Starter
       #13

    Thanks I'm going to try it but I see a problem, you wrote when program 2 is closed, program 1 will close, right? It's the opposite, since program 1 is the game, that's this one I'll close manually...
      My Computer


  4. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #14

    unko said:
    Thanks I'm going to try it but I see a problem, you wrote when program 2 is closed, program 1 will close, right? It's the opposite, since program 1 is the game, that's this one I'll close manually...
    I really cannot see what's the issue? The same script, now using CPU Throttle and Game as example programs:
    Code:
    [System.Diagnostics.Process]::Start("Path_to_CPU Throttle")
    [System.Diagnostics.Process]::Start("Path_to_Game").WaitForExit()
    taskkill /IM Name_of_the_process_CPU Throttle
    exit
    That's it. CPU Throttle and the Game are launched. When the Game is manually closed, system stops the CPU Throttle. Exactly what you wanted:
    unko said:
    Anytime i launch a game, i want CPU Throttle to launch. And when i close that game, i want it to close too!
    About one of your requirements:
    unko said:
    Really need a script that's always running and monitoring, automatically launched at boot.
    This is absolutely unnecessary. The script I have given to you launches both CPU Throttle and the Game, then closes CPU Throttle when the Game is manually closed.

    Kari

    Kari
      My Computer


  5. Posts : 289
    Win7x64
    Thread Starter
       #15

    I just tried to launch it but got error, checked and it says "use of script is disabled on this system". Also .PS1 extension is by another app (PSXMemTool) but i guess that's not important. There's something i'm not sure of still though : When you launch this script, it'll automatically launch both of the apps and close the one of your choice in the end, right?

    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
      My Computer


  6. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #16

    unko said:
    I just tried to launch it but got error, checked and it says "use of script is disabled on this system". Also .PS1 extension is by another app (PSXMemTool) but i guess that's not important. There's something i'm not sure of still though : When you launch this script, it'll automatically launch both of the apps and close the one of your choice in the end, right?
    Right.

    I forgot to mention that scripts must first be enabled. As I always enable the PowerShell scripts I simply forgot the whole thing.

    I am not at home now, please give me an hour or two and I will edit my original post accordingly to show how to enable scripting. In the mean time, here's something for you to read:


    unko said:
    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
    With native Windows batch and scripting tools, to achieve what you want to is impossible, as far as I know. In my opinion the better alternative is to prepare shortcuts for each game, each shortcut launching a script that starts CPU Throttle and the respective game, then closes CPU Throttle when the game is closed.

    Kari
      My Computer


  7. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #17

    OK, the original post is now more like a mini tutorial, it is edited to cover everything you need to know to enable PowerShell scripts and create a script that launches multiple programs and closes them all when the last program launched by the script is manually closed.

    Kari
      My Computer


  8. Posts : 289
    Win7x64
    Thread Starter
       #18

    Kari said:
    unko said:
    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
    With native Windows batch and scripting tools, to achieve what you want to is impossible, as far as I know. In my opinion the better alternative is to prepare shortcuts for each game, each shortcut launching a script that starts CPU Throttle and the respective game, then closes CPU Throttle when the game is closed.

    Kari
    Hmm ok well thanks for all, i'll have a look into it, and if someone knows a way to do the full thing i wanted, let us know. I guess it would be an app doing that then, not a script.
      My Computer


  9. Posts : 5,092
    Windows 7 32 bit
       #19

    Take a look at RunGroup
      My Computer


  10. Posts : 289
    Win7x64
    Thread Starter
       #20

    Link's dead ands I cannot find it anywhere else Looked good..
      My Computer


 
Page 2 of 3 FirstFirst 123 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 16:16.
Find Us