I am using an external script (SupportFile.ahk) to show activity while the main script is uploading a file to Autohotkey.net.
The SupportFile.ahk starts dots moving back and forth on a Gui created by the main script. Those dots are frozen while the FTP functions are uploading the file.
I know that the FTP functions cause the same script to wait until the FTP functions are completed before continuing. Including timers and loops, but why does it freeze the Gui from being interacted with by an external script?
Fully functional example below. Just put in your AutoHotkey username, password and choose a file to upload.
A work around is to put the Gui in the external script and show it by either passing a variable to the other scirpt or writing/reading to an ini file.
Main Script
Code:
;FTP functions by Olfen
;http://www.autohotkey.com/forum/topic10393.html
#SingleInstance, force
run SupportFile.ahk
AutoHotkeyUsername = ;add Username
AutoHotkeyPassword = ;add password
UploadFile = ; add upload file
SplitPath,UploadFile,,,,NewUploadDirectoryName ;Name for new AutoHotkey.net test directory
Gui, font, s10
Gui, add, text,x15 y10, Test Script
Gui, add, button,x15 y35, Start Upload
Gui, add,text,x15 y73,Uploading:
Gui, font,s32
Gui, add,text,x120 y56 vUploadingDot,ยท
Gui, show, y200 w400 h100 , Test Script
Gui, color, blue
winset,alwaysontop,On,Test Script
WinSet, Region, 0-0 w400 h100 R40-40, Test Script
Gui, -caption
return
GuiEscape:
exitapp
ButtonStartUpload:
FtpConnection := FtpOpen("autohotkey.net", "21", AutoHotkeyUsername, AutoHotkeyPassword)
If (FtpConnection != 1)
{
MsgBox,16,Error, Not able to upload file.`nMake sure you are connected to the internet and try again.`nError line number %A_LineNumber%
return
}
FtpCreateDirectory( NewUploadDirectoryName )
FtpSetCurrentDirectory( NewUploadDirectoryName )
FileTransfer := FtpPutFile( UploadFile , UploadFile )
If (fileTransfer != 1)
{
MsgBox,16,Error, Not able to upload file.`nMake sure you are connected to the internet and try again.`nError line number %A_LineNumber%
return
}
FtpClose()
MsgBox Done
exitapp
Return
;Contents of FTP.ahk is below
/*
http://msdn.microsoft.com/library/en-us/wininet/wininet/ftp_sessions.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetopen.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetconnect.asp
*/
FtpCreateDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpCreateDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpRemoveDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpRemoveDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpSetCurrentDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpPutFile(LocalFile, NewRemoteFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewRemoteFile=
NewRemoteFile := LocalFile
global ic_hInternet
r := DllCall("wininet\FtpPutFileA"
, "uint", ic_hInternet
, "str", LocalFile
, "str", NewRemoteFile
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpGetFile(RemoteFile, NewFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewFile=
NewFile := RemoteFile
global ic_hInternet
r := DllCall("wininet\FtpGetFileA"
, "uint", ic_hInternet
, "str", RemoteFile
, "str", NewFile
, "int", 1 ;do not overwrite existing files
, "uint", 0 ;dwFlagsAndAttributes
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpGetFileSize(FileName, Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
global ic_hInternet
fof_hInternet := DllCall("wininet\FtpOpenFileA"
, "uint", ic_hInternet
, "str", FileName
, "uint", 0x80000000 ;dwAccess: GENERIC_READ
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or fof_hInternet = 0)
return -1
FileSize := DllCall("wininet\FtpGetFileSize", "uint", fof_hInternet, "uint", 0)
DllCall("wininet\InternetCloseHandle", "UInt", fof_hInternet)
return, FileSize
}
FtpDeleteFile(FileName) {
global ic_hInternet
r := DllCall("wininet\FtpDeleteFileA", "uint", ic_hInternet, "str", FileName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpRenameFile(Existing, New) {
global ic_hInternet
r := DllCall("wininet\FtpRenameFileA", "uint", ic_hInternet, "str", Existing, "str", New)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpOpen(Server, Port=21, Username=0, Password=0 ,Proxy="", ProxyBypass="") {
IfEqual, Username, 0, SetEnv, Username, anonymous
IfEqual, Password, 0, SetEnv, Password, anonymous
If (Proxy != "")
AccessType=3
Else
AccessType=1
;#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
;#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
;#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
;#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
global ic_hInternet, io_hInternet, hModule
hModule := DllCall("LoadLibrary", "str", "wininet.dll")
io_hInternet := DllCall("wininet\InternetOpenA"
, "str", A_ScriptName ;lpszAgent
, "UInt", AccessType
, "str", Proxy
, "str", ProxyBypass
, "UInt", 0) ;dwFlags
If (ErrorLevel != 0 or io_hInternet = 0) {
FtpClose()
return 0
}
ic_hInternet := DllCall("wininet\InternetConnectA"
, "uint", io_hInternet
, "str", Server
, "uint", Port
, "str", Username
, "str", Password
, "uint" , 1 ;dwService (INTERNET_SERVICE_FTP = 1)
, "uint", 0 ;dwFlags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or ic_hInternet = 0)
return 0
else
return 1
}
FtpClose() {
global ic_hInternet, io_hInternet, hModule
DllCall("wininet\InternetCloseHandle", "UInt", ic_hInternet)
DllCall("wininet\InternetCloseHandle", "UInt", io_hInternet)
DllCall("FreeLibrary", "UInt", hModule)
}
SupportFile.ahk
Code:
#SingleInstance,force
UploadingForward = 1
UploadingDotXPos = 110
Loop
{
If UploadingForward = 1
UploadingDotXPos := ( UploadingDotXPos + 3 ) ;if going forward add 3
If UploadingForward = 0
UploadingDotXPos := ( UploadingDotXPos - 3 ) ;if going backward substract 3
ControlMove, Static3, %UploadingDotXPos%, 56, , , Test Script
if ( UploadingForward = 1 and UploadingDotXPos > 370 )
UploadingForward = 0 ;reverse direction of dots of Xpos is greater then 370
if ( UploadingForward = 0 and UploadingDotXpos < 110 )
UploadingForward = 1 ;reverse direction if Xpos is less then 90
}
DataLife