Sign in to follow this  
Followers 0
LarsBNygaard

How do I terminate a running program with AI?

8 posts in this topic

I'm using Win7, Firefox 3.6.13 (now updating to 3.6.16) and BitTorrent 6.4 (and a lot of other programs, but these are the essential ones).

Now, to improve my browsing speed, I'd like to make an AutoIt script, which will either set all torrents to 'pause' (the preferred choice) or simply terminate BitTorrent, (if the 'pause-option' is unavailable), when ever I open a Firefox-window, and - upon me closing all firefox-windows - start BitTorrent again (or simply re-start the up/downloads).

Could someone please help me to find out how to pause/close Bittorrent, when clicking the script, which will then open Firefox?

(I'm usually running BitTorrent 'minimized to tray'...)

BitTorrent: "C:\Program Files\BitTorrent\bittorrent.exe" /NOINSTALL

Firefox: "C:\Program Files\Mozilla Firefox\firefox.exe"

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm using Win7, Firefox 3.6.13 (now updating to 3.6.16) and BitTorrent 6.4 (and a lot of other programs, but these are the essential ones).

Now, to improve my browsing speed, I'd like to make an AutoIt script, which will either set all torrents to 'pause' (the preferred choice) or simply terminate BitTorrent, (if the 'pause-option' is unavailable), when ever I open a Firefox-window, and - upon me closing all firefox-windows - start BitTorrent again (or simply re-start the up/downloads).

Could someone please help me to find out how to pause/close Bittorrent, when clicking the script, which will then open Firefox?

(I'm usually running BitTorrent 'minimized to tray'...)

BitTorrent: "C:\Program Files\BitTorrent\bittorrent.exe" /NOINSTALL

Firefox: "C:\Program Files\Mozilla Firefox\firefox.exe"

This code works for uTorrent and Firefox - Replace BitTorrent Process Name to adapt it for BitTorrent:

;Get PIDs
    Local $FirefoxPID = ProcessExists("firefox.exe")
    Local $uTorrentPID = ProcessExists("utorrent.exe")

    ;If uTorrent Exists, kill it and start firefox
    If $uTorrentPID <> 0 Then
        ProcessClose($uTorrentPID) ;If uTorrent exists, kill it.
        Run("C:\Program Files\Mozilla Firefox\firefox.exe") ;run firefox after killing uTorrent
    EndIf

    ;If firefox does not exist, start uTorrent
    If $FirefoxPID == 0 Then
        Run("C:\Program Files\BitTorrent\bittorrent.exe")
    EndIf


    Sleep(200) ;We need to give the processor some rest!

Please feel free to comment on the code. Thank you.

Edited: Removed the endless loop - it would have caused the system to cycle between uTorrent and Firefox! :)

Edited by Zulqarnain

Share this post


Link to post
Share on other sites

Hi Zulqarnain,

Thankyou for the suggestion.

This is the code I try to use, but it does absolutely nothing, what am I missing..?

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1

Author: myName

Script Function:

Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

;WinClose(" ;Get PIDs

Local $FirefoxPID = ProcessExists("firefox.exe")

Local $uTorrentPID = ProcessExists("utorrent.exe")

;If uTorrent Exists, kill it and start firefox

If $uTorrentPID <> 0 Then

ProcessClose($bittorrentPID) ;If bittorrent.exe exists, kill it.

Run("C:\Program Files\Mozilla Firefox\firefox.exe") ;run firefox after killing bittorrent.exe

EndIf

;If firefox does not exist, start bittorrent

If $FirefoxPID == 0 Then

Run("C:\Program Files\BitTorrent\bittorrent.exe")

EndIf

Share this post


Link to post
Share on other sites

OK.. I found one error - I hadn't changed all the utorrent to bittorent, but none the less; the script DOES open yet another instance of bittorrent, which is NOT the idea, and it does NOT open Firefox..???

Code:

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1

Author: myName

Script Function:

Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

;WinClose(" ;Get PIDs

Local $FirefoxPID = ProcessExists("firefox.exe")

Local $bittorrentPID = ProcessExists("bittorrent.exe")

;If bittorrent Exists, kill it and start firefox

If $bittorrentPID <> 0 Then

ProcessClose($bittorrentPID) ;If bittorrent.exe exists, kill it.

Run("C:\Program Files\Mozilla Firefox\firefox.exe") ;run firefox after killing bittorrent.exe

EndIf

;If firefox does not exist, start bittorrent

If $FirefoxPID == 0 Then

Run("C:\Program Files\BitTorrent\bittorrent.exe")

EndIf

Share this post


Link to post
Share on other sites

I'm not sure why it's not killing the bittorrent process, but it will always restart bittorrent as long as Firefox wasn't running at the start of the script. You only check to see if Firefox is running once at the start of the script and never check for it again. You need to put your Firefox test in an endless loop that only exits that loop when Firefox is closed, or use RunWait when you start Firefox so the script pauses until you've finished using Firefox.

Also, take the Run command out of the If $bittorrentPID <> 0 Then...EndIf loop because if bittorrent isn't running, firefox will not be started.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Well, here we are at the limits f my understanding, but I guess that I've got the point..

Would it be possible to implement a 'wait' command, somewhat like this:

-----------------------------

;WinClose(" ;Get PIDs

Local $FirefoxPID = ProcessExists("firefox.exe")

Local $bittorrentPID = ProcessExists("bittorrent.exe")

;If bittorrent Exists, kill it and start firefox

If $bittorrentPID <> 0 Then

ProcessClose($bittorrentPID) ;If bittorrent.exe exists, kill it.

Run("C:\Program Files\Mozilla Firefox\firefox.exe") ;run firefox after killing bittorrent.exe

EndIf

; Wait 10 seconds

Opt("WinWaitDelay", 10000)

;If firefox does not exist, start bittorrent

If $FirefoxPID == 0 Then

RunWait("C:\Program Files\BitTorrent\bittorrent.exe")

EndIf

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I don't have bittorrent on this PC to test, but shouldn't this handle it?

I assume you want to automatically stop & start bittorrent, based upon when YOU open or close Firefox.

This should be able to run pretty much perpetually in the background...

While True
    
    Local $FirefoxPID = ProcessExists("firefox.exe")
    Local $bittorrentPID = ProcessExists("bittorrent.exe")

    ;If Firefox Exists, and bittorrent is running, kill bittorrent
    If $bittorrentPID <> 0 And $FirefoxPID <>  0 Then
        ProcessClose($bittorrentPID) ;If bittorrent.exe exists, kill it.
    EndIf

    ;If firefox does not exist, and bittorrent is not running, start bittorrent
    If $FirefoxPID == 0 And $bittorrentPID == 0 Then
        Run("C:\Program Files\BitTorrent\bittorrent.exe")
    EndIf 

    ;wait around for Firefox to start or close
    Sleep(1000)
WEnd
Edited by bwochinski

Share this post


Link to post
Share on other sites

Well, I guess that I cracked the nut (in a manner of speaking).

Apparently bittorent has a rather timeconsuming 'shut-down-procedure' - at least on my PC, from the "exit-click" is done, until the process is actually disappearing from Windows joblist, app. 12½-13½ seconds pass.

In order to overcome this, I've added the 'wait' command, in order to pause the script for 15 seconds - after starting firefox - before checking if firefox is still running; and have subsequently adapted the "RunWait", in order to have the script restart bittorrent after Firefox has been closed.

In effect this works like a dream for me ;oD

I'm very grateful for all your help, and post the code for anyone to use!

CODE-START---------------------------

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1

Author: Lars Bech Nygaard with kind help from BrewManNH and Zulqarnain

Script Function:

Terminate Bittorrent, Run Firefox and restart Bittorent on exit from Firefox

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

;WinClose(" ;Get PIDs

Local $FirefoxPID = ProcessExists("firefox.exe")

Local $bittorrentPID = ProcessExists("bittorrent.exe")

;If bittorrent Exists, kill it and start firefox

If $bittorrentPID <> 0 Then

ProcessClose($bittorrentPID) ;If bittorrent.exe exists, kill it.

Run("C:\Program Files\Mozilla Firefox\firefox.exe") ;run firefox after killing bittorrent.exe

EndIf

; Wait 15 seconds

Opt("WinWaitDelay", 15000)

;If firefox does not exist, start bittorrent

If $FirefoxPID == 0 Then

RunWait("C:\Program Files\BitTorrent\bittorrent.exe")

EndIf

CODE-END---------------------------

Yours most grateful

Lars Bech Nygaard

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0