Jump to content

How do I Gracefully Close an AutoIT script that is running?


Recommended Posts

Hello,

I've been trying to end an AutoIt script that has been running by some external method. I have not 

I have tried Powershell: "Stop-Process AutoIt3" and the script continues to run. If I compile the script it will Stop, but the OnAutoITExit() function does not get executed. 

The command line "taskkill" also ends the process, but not gracefully.

Can anyone tell me how to close a script and have it execute the OnAutoITExit() function from a command line, or programmatically?

Thanks

Link to comment
Share on other sites

I did, but it just clicks around the application forever. 

I usually close it from the system tray icon. I wrote an OnAutoITExit() function which just writes to a log file how many times the loop executed and the time. 

$intCount = 0    ; number of iterations through the loop
Do
$intCount = $intCount + 1 ; increment Loop Iteration Count

Until

Link to comment
Share on other sites

  • Developers

Have a look at this example:

The script to compile, then Run and you like to close gracefully:

OnAutoItExitRegister("OnAutoItExit")
AutoItWinSetTitle("testing")
While 1
    sleep(50)
WEnd


Func OnAutoItExit()
    MsgBox(0,"testing closing","test")
EndFunc   ;==>OnAutoItExit

 

The AutoIt3 command to close it gracefully:

WinClose("testing")

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok, that posted too soon.

I meant to say that I did write the script. I plan to kick it off remotely, but don't now. And here is an edited version:

$strFileName = "AutoIT Log " & @ComputerName & " " & @ScriptName & " " & @YEAR & @MON & @MDAY & "-" & @HOUR & @MIN & @SEC &".txt"
$file = FileOpen($strFileName, 1) 

$bEndLoop = False
$intCount = 0   ; number of iterations through the loop
Do
    MouseClick( "primary", 483, 561) 
    MouseClick( "primary", 936, 459) 
    $intCount = $intCount + 1 ; increment Loop Iteration Count
    
Until $bEndLoop

Func OnAutoItExit ()
    If $file = -1 Then  ; Check if file opened for writing OK
        MsgBox(0, "Error", "Unable to open file " & $strLogFileName)
        Exit
    EndIf

    MyLogInfo("", $strFileName) ; blank line
    MyLogInfo($strMessage, $strFileName)    
    MyLogInfo("Runtime is: " & $intRunTime & " ms",  $strFileName)
    MyLogInfo("", $strFileName) ; blank line
    MyLogInfo("End Script:" & @MON & "-" & @MDAY & "-" & @YEAR & " - " & @ScriptName, $strFileName)

EndFunc ;OnAutoItExit

or something close to that...

Link to comment
Share on other sites

It actually clicks on a lot of buttons while displaying "progress until" and "location of" the next click, but that's really all it does.

I have a client application running on several VMs.

Currently, I

  1. Start the server and clients and AutoIT on each VM manually by clicking a button on my client. The AutoIT script runs starts clicking on client buttons.
  2. I start perfmon on all the machines via a .cmd file.
  3. Everything runs overnight.
  4. I stop perfmon on all the machines via a different .cmd file.
  5. Then I stop the AutoIt on all the VMs by manually navigating to each VM clicking the AutoIT system tray icon, and  clicking Exit. Then I review the performance. 

This assumes my clients and server(s) all ran overnight!   ;-)  

I would like to automate all of this.

Link to comment
Share on other sites

15 minutes ago, Jos said:

So, did you see my initial post in this thread on how it can be done?

Jos

Yes and it works!

But only after I compiled my ButtonClick.au3 script.

I can work with that. Thank you very much, Jos!

 

As an aside: When I ran ButtonClick.au3 as a script it did not. If you really want to get into it, I'm game.

Otherwise, I'm happy with compiling it. (My short test client worked as a script. I'm sure yours does as well.)

It may be because of a function I wrote which Delays between clicks.

Here is the function which Delays between clicks:

;---------- Some variables I used. Hopefully that's all you need. (There might be an extra one.) ----------------------
$bWaitforStartup = False    ; True if we allow for a StartUpDelay pause before starting automation
$intStartUpDelay = 60000    ; original delay 60s waiting for system to boot up and launch PQual_C client
$intLoopDelay = 6000    ; original delay 12s in ms 
$intSMousDelay = 20     ; original delay  3s in sec
$intCount = 0   ; number of iterations through the loop

;---------- place in Do loop ----------------------
    ; Start Timer after first click
    if $bTimerStarted == False Then
        $timerBeginClicks = TimerInit()
        $bTimerStarted = True
        $intRunTime = TimerDiff($timerBeginClicks) ; time since $timerBeginClicks was initialized
        $intRuntime = Round( $intRuntime, 0)
        MyLogInfo("Start Timer", $strFileName)  ; blank line
    EndIf

;---------- place before each click ----------------------
Func MyMsgProgressBox($strMsg, $intDelayTime)
    
    If ($intDelayTime <= 0) Then
        $intDelayTime = 5
    EndIf
    
    ; Global variable $strMessage
    $strMessage = "Loop " & $intCount & " " & $strMsg
    ProgressSet( 0, $intDelayTime & " seconds")

    $sleepDelayIncrement = ((1000-5)*(($intDelayTime)/($intDelayTime+1)))
    
    if $bTimerStarted Then
        $intRunTime = TimerDiff($timerBeginClicks) ; time since $timerBeginClicks was initialized
        $intRuntime = Round( $intRuntime, 0)
        $strTitle= " (" & $intRunTime & " ms) " & "Loop " & $intCount & " - Clicking in " & $intDelayTime & " seconds" ; Show the ms and loop count indented a bit, and the total delay time
    Else
        $strTitle= " Loop " & $intCount & " - Clicking in " & $intDelayTime & " seconds" ; Show loop count indented a bit, and the total delay time
    EndIf 

    $strMainText =  $strMsg
    $strSubText = 0 & " seconds" 
    if $debug Then
        ProgressOn( $strTitle, $strMainText, $strSubText)
    EndIf 
    
    For $i = 0 to $intDelayTime step 1
        $strSubText = $i & " seconds"

        if $debug Then
                ProgressSet( 100*(($i)/($intDelayTime)), $strSubText, $strMainText)
        Endif 
    
    sleep($sleepDelayIncrement)
    Next
    ProgressOff()
EndFunc ; MyMsgProgressBox($strMesage, $intDelayTime)

The delay function is not quite accurate and I can't move the progress box around, but that's not all that important yet. It's good enough until I have time to enhance it.

I should probably start another thread.

Regards

 

Link to comment
Share on other sites

With scripts written in other languages, I quite often have the script when it starts create a flagfile on the desktop of the user who is running the script.  The flagfile usually contains the name of the script concerned, the date & time it started, the computer-name & process-id it itself is running under (useful for debugging other stuff it does) and sometimes an indication of the parameters the script is running with.  That complexity means that I could have several instances of the script running at once, and will be able to tell which flagfile relates to which script, and if the flagfiles are synced around via Dropbox, I can see multiple machines' flagfiles from any machine.  A typical flagfile name might then be:

   ~scriptname flagfile for '+!mup' z=5 on DELL-650 Administrator 20160121 164543 p=3108.txt

The script then tests if that file exists every so often.  The user can stop any such script by deleting the file.  If the script stops for some other reason it deletes the file too.

 

 

Link to comment
Share on other sites

  • 4 years later...
On 5/9/2016 at 7:31 PM, Jos said:

Have a look at this example:

This seems to work with both scripts not compiled:

00.au3

;Help File: AutoItWinSetTitle

#include <GUIConstantsEx.au3>
#include <Misc.au3>

Global $Title = "MyTitle"

;only one instance of the script may be running
_Singleton($Title, 0)

OnAutoItExitRegister("OnAutoItExit")

AutoItWinSetTitle($Title)

;Display AutoIt's Hidden Window. Returns the handle of the window.
Global $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window.
WinMove($hWnd, "", (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ; Move the AutoIt Hidden Window and re-size for a better view.
WinSetState($hWnd, "", @SW_SHOW) ; Show the AutoIt Hidden Window, normally this is hidden, but in the interest of this example I"m displaying it.

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch
WEnd

01.au3

;Help File: _Singleton
;Jos, May 9 2016
;https://www.autoitscript.com/forum/topic/182398-how-do-i-gracefully-close-an-autoit-script-that-is-running/?do=findComment&comment=1309935

#include <Misc.au3>

Global $Title = "MyTitle"

If _Singleton($Title, 1) = 0 Then
   ;Msgbox(48,"Atención", "Ya se está ejecutando." & @LF & "Esta instancia se cerrará.", 5)
   ;Exit
   ;WinClose($Title)
   Local $hWnd = WinGetHandle($Title)
   WinClose($hWnd)
EndIf

After some tests: seems to work better just WinClose (without the If _Singleton ...)

;Jos, May 9 2016
;https://www.autoitscript.com/forum/topic/182398-how-do-i-gracefully-close-an-autoit-script-that-is-running/?do=findComment&comment=1309935
Global $Title = "MyTitle"
WinClose($Title)
Edited by robertocm
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...