RunWait timeout?

Get help with using AutoHotkey and its commands and hotkeys
diaewad

RunWait timeout?

06 Feb 2015, 09:49

I'm trying to figure out a timeout function for RunWait. I checked the help file but couldn't quite figure it out. I have a simple code to grab the ip address off and excel spreadsheet and ftp to it, but after I use it once or twice, it just stops working. I'm guessing it's because of a timeout problem, but I don't know for certain. Here's my code:

Code: Select all

#F::
Send ^c
Sleep, 150
RunWait, %comspec% /c ftp %clipboard%
Return
If anyone knows of another reason why it happens, please let me know. Thanks!
User avatar
TLM
Posts: 1564
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: RunWait timeout?

06 Feb 2015, 12:48

It seems likely that Excel is loosing focus and or the cell contents are not being copied to the clipboard.
You could use COM to create a more reliable means of getting the ip address.
Something like:

Code: Select all

book := A_ScriptDir "\book1.xls" ; change this to the path of your excel file
cell = A1 			  ; change this to the cell containing the IP address
xlObj := ComObjGet( book )
ip := xlObj.Sheets( 1 ).Range( cell ).Value

RunWait, %comspec% /c ftp %ip%
;..............
That said, this should kill the FTP executed by RunWait after 3 seconds
( make sure you're running AHk with Admin privileges on! )

Code: Select all

#F::
Send ^c
Sleep, 150
SetTimer, Kill_FTP, -3000 ; change 3000 to in/decrease the amount of time to wait.
RunWait, %comspec% /c ftp %clipboard%,, UseErrorLevel, ftpPID

; place the rest of your script here

Return

Kill_FTP:
If ( ErrorLevel != 0 )
    WinKill % "ahk_pid " ftpPID
Return
You could also check the clipboard to make sure it has an IP address on it before trying the RunWait

Code: Select all

#F::
Send ^c
Sleep, 150
if !( clipboard ~= "^(\d{1,3}\.){3}\d{1,3}" )
{
    Msgbox, 0x10, Whoops!, There is no IP Address on the clipboard! Please try again.
    Return
}

SetTimer, Kill_FTP, -3000 ; change 3000 to in/decrease the amount of time to wait.
RunWait, %comspec% /c ftp %clipboard%,, UseErrorLevel, ftpPID

; place the rest of your script here

Return

Kill_FTP:
If ( ErrorLevel != 0 )
    WinKill % "ahk_pid " ftpPID
Return
just some ideas to try..
diaewad

Re: RunWait timeout?

06 Feb 2015, 14:06

That all looks awesome. I have to use this for my spreadsheet with over 600 entries that I then have to update, so COM wouldn't work here, but that will help me in a MASSIVE way in the future, so I thank you greatly for that!!
I know it's grabbing the data, because the Excel cell always blinks once it transfers to the clipboard, plus even if it was empty, it would just bring up the command prompt with an ftp> prompt (this has happened to me already)

I will certainly try the timeout.

Thank you so much for your help!
A_User
Posts: 36
Joined: 21 Aug 2017, 01:15

Re: RunWait timeout?

20 Nov 2017, 11:29

Another approach I found is to use `Process, WaitClose`.

Code: Select all

#SingleInstance, Force
#Persistent

new RunWait( 3 ).run( "notepad" )
msgbox done
Return

class RunWait {

    iTimeout := 10

    __New( iTimeout=10 ) {
        this.iTimeout := iTimeout
    }

    run( sCommand ) {
    
        Run, % sCommand,, UseErrorLevel, _iPID        
        _func   := ObjBindMethod( this, "closeProcess", _iPID )
        SetTimer, % _func, % this.iTimeout ? this.iTimeout * -1000 : -1
        Process, WaitClose, % _iPID, % this.iTimeOut
        
    }
        
    closeProcess( iPID ) {
        Process, Close, % iPID
    }

}

Return to “Ask For Help”

Who is online

Users browsing this forum: Choueiry, JnLlnd, jwinfl, lmstearn, Snova and 267 guests