I have a script that helps me download data from a stock charting program.
A command needs to be sent to the program for each stock one at a time and then wait for the download to complete.
I have a simple GUI to choose symbols and date ranges.
The logic I am using is:
Click Download Button -> Download Data (using sleep to wait a specified number of seconds)
The issue I am having is that some stocks download much more quickly than others. If I would like to be able to skip to the next symbol by clicking a button on the Gui, I don't know how to do that since the script is in sleep mode.
How else can I accomplish this?
If it helps, here is the undoubtedly inefficient script

:
Code:
#SingleInstance force
#NoTrayIcon
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 1
FileReadLine, Xpos, NTReplayDownloader.ini, 1
FileReadLine, Ypos, NTReplayDownloader.ini, 2
ActiveDownload = 0
Gui, Add, MonthCal, x142 y40 w240 h170 vStartDate
Gui, Add, MonthCal, x392 y40 w240 h170 vEndDate
Gui, Add, ListBox, R19 vSymbol x10 y45 w120 Multi AltSubmit,
InstrumentsLoop = 0
Loop, Read, Instruments.txt
{
InstrumentsLoop += 1
GuiControl, , Symbol, %A_loopReadLine%|
Ticker%InstrumentsLoop% := A_LoopReadLine
NumberOfSymbols := A_Index
}
Gui, Add, Button, x300 y239 w80 h30 Default, Download
Gui, Add, Button, x390 y239 w40 h30 , Stop
;Gui, Add, Button, x435 y239 w40 h30 , Go
Gui, Add, Button, x18 y15 w100 h20 , Reload Symbols
Gui, Add, Text, x182 y20 w160 h20 +Center, Start Date
Gui, Add, Text, x432 y20 w160 h20 +Center, End Date
Gui, Add, Text, x212 y220 w90 h20 , Seconds To Wait:
Gui, Add, Edit, x302 y217 w76 h20 vWaitSeconds, 150
Gui, Add, Text, x182 y285 w408 h14 +Center vNotify,
Gui, Add, CheckBox, x395 y214 w105 h26 vShowTips checked, Show TrayTips
Gui, Add, Text, x182 y271 w408 h14 +Center vTemp, this is a BETA version, use Ctrl-Alt-Del if program hangs
Gui, Show, x%Xpos% y%Ypos% h306 w635, NT Replay Downloader
return ; Gui
ButtonStop:
BlockInput, Off
SetTimer, Countdown, Off
GuiControl,, Notify, Stopped!
Gui, Color, Default
ActiveDownload = 0
loopDate = %StartDate%
;Pause
Stopped := true
return ; ButtonStop
ButtonGo:
goto, ButtonDownload
return
ButtonReloadSymbols:
GuiControl,, Notify, Reloading
GuiControl, , Symbol, |
SymbolLoop = 0
Loop, Read, Instruments.txt
{
SymbolLoop += 1
GuiControl, , Symbol, %A_loopReadLine%|
Ticker%SymbolLoop% := A_LoopReadLine
NumberOfSymbols := A_Index
}
GuiControl,, Notify, Symbols Reloaded
;msgbox % NumberOfSymbols
return ; ButtonReloadSymbols
ButtonDownload:
;msgbox, Ha!, you clicked didn't you!
Stopped := false
Gui, Submit, NoHide
calcWaitSeconds := WaitSeconds*1000
;NTdown = 0;
LoopCount = 0
Loop, parse, Symbol, |
{
;if LoopCount = ; see http://www.autohotkey.com/docs/Variables.htm
;{
;msgbox, empty
;LoopCount := 1
;}
;msgbox % StartDate
if (Stopped = true)
break
LoopCount := LoopCount + 1 ; Keep track where we are in the loop in case Download button gets pressed again.
;msgbox, %LoopCount%
;msgbox, %a_index%
if (LoopCount < A_Index)
{
continue ;skip this if it has already been done.
}
thisSymbol := A_LoopField ; Store this position number in the next array element.
NTdown := A_LoopField - 1 ; Store this position number in the next array element.
if (thisSymbol <> "")
{
ActiveDownload = 0
Gosub, WaitForNT
}
} ; Symbol parsing loop
LoopCount = 0 ; see arrays in help file
SetTimer, Countdown, Off
BlockInput, Off
if (Stopped <> true) ; leave the 'Stopped!' text there if user intervened
{
GuiControl,, Notify, Task Complete!
}
Gui, Color, Default
ActiveDownload = 0
;loopDate = %StartDate%
If (IsError = true)
{
MsgBox, 48, Error Log, NT Replay Downloader`n`nSome data was not available`nsee NTReplayDownloader-log.txt`nin installation folder
}
Return ; ButtonDownload
WaitForNT:
if (Stopped = 1)
{
return
}
Gui, Submit, NoHide
calcWaitSeconds := WaitSeconds*1000
BlockInput, On
IfWinExist, Download Replay Data
{
WinClose, Download Replay Data
}
if ActiveDownload = 0
{
GuiControlGet, SymbolText,,Symbol,Text
Gui, Color, FFD334
Diff := EndDate
Diff -= StartDate, days
if EndDate < %StartDate%
{
BlockInput, Off
msgbox, Please select a valid date range
SetTimer, CountDown, Off
BlockInput, Off
Gui, Color, Default
ActiveDownload = 0
loopDate = %StartDate%
return
}
Diff := 1000 / %Diff%
GuiControl,, DLprogress, %Diff%
if (thisSymbol = "")
{
BlockInput, Off
msgbox, Please select a symbol
SetTimer, CountDown, Off
BlockInput, Off
Gui, Color, Default
ActiveDownload = 0
loopDate = %StartDate%
return
}
loopDate = %StartDate%
StringRight, StartDay, loopDate, 2
StringMid, StartMonth, loopDate, 5, 2
StringLeft, StartYear, loopDate, 4
StringRight, EndDay, EndDate, 2
StringMid, EndMonth, EndDate, 5, 2
StringLeft, EndYear, EndDate, 4
IfWinExist, Download Replay Data
{
WinClose, Download Replay Data
}
ActiveDownload = 1
}
while loopDate <= EndDate
{
if (Stopped = 1)
{
break
}
Gui, Submit, NoHide
calcWaitSeconds := WaitSeconds*1000
WinWait, Control Center,
IfWinNotActive, Control Center, , WinActivate, Control Center,
WinWaitActive, Control Center,
ifWinExist, Replay Data
WinClose, Replay Data
ifWinNotExist, Download Replay Data
{
Send, {ALTDOWN}f{ALTUP}ud
WinWait, Download Replay Data,
IfWinNotActive, Download Replay Data, , WinActivate, Download Replay Data,
WinWaitActive, Download Replay Data,
}
Send {Tab}{Down %NTdown%}{Tab}
Send %StartMonth%{Right}%StartDay%{Right}%StartYear%
BlockInput, Off
if ShowTips = 1
{
traytip, Downloading, % Ticker%thisSymbol% . ": " . StartMonth . "/" . StartDay . "/" . StartYear
}
Sleep, 1000
Send {Tab 3}{Enter}{Tab 2}
StringMid, NotifyDay, loopDate, 7, 2
StringMid, NotifyMonth, loopDate, 5, 2
StringLeft, NotifyYear, loopDate, 4
loopDate += 1, days
;NOTE ABOVE CONVERTS TO FULL DATETIME FORMAT
StringMid, StartDay, loopDate, 7, 2
StringMid, StartMonth, loopDate, 5, 2
StringLeft, StartYear, loopDate, 4
StringLeft, loopDate, loopDate, 8
BlockInput, Off
GuiControl,, DLprogress, %Diff%
SecondsRemaining := WaitSeconds
SetTimer, Countdown, 1000
Sleep, %calcWaitSeconds%
IfWinExist, Error
{
FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ", Connection to data server is busy or not available right now.", NTReplayDownloader-Log.txt
IsError := true
WinClose, Error
}
IfWinExist, Replay Data, no L1
{
FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ",No L1 Data (note L1 error also prevents L2 from downloading even if available)", NTReplayDownloader-Log.txt
IsError := true
WinClose, Replay Data
}
IfWinExist, Replay Data, no L2
{
FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ",No L2 Data, NTReplayDownloader-Log.txt"
IsError := true
WinClose, Replay Data
}
} ; while loop
loopDate = %StartDate%
Return ; WaitForNT
Countdown:
if (SecondsRemaining >= 1)
{
SecondsRemaining := SecondsRemaining - 1
}
else
{
SecondsRemaining := 0
}
GuiControl,, Notify, % "Sent " . ticker%thisSymbol% . ": " . NotifyMonth . "/" . NotifyDay . "/" . NotifyYear . " -> " . SecondsRemaining . "."
Return ; Countdown
GuiClose:
gosub, WriteToFile
ExitApp
Return
WriteToFile:
FileDelete, NTReplayDownloader.ini
WinGetPos,X,Y,W,H,NT Replay Downloader
FileAppend,%X%`n%Y%, NTReplayDownloader.ini
Return ; WriteToFile