mulevad

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

11 posts in this topic

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

Share this post


Link to post
Share on other sites



What's the script? Did you write it? Is it being started on its own?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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...

Share this post


Link to post
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.

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

Actually you only need to call the function before each click!

Share this post


Link to post
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.

 

 

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