AutoHotkey Community

It is currently May 27th, 2012, 11:09 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: January 18th, 2012, 8:32 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
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

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Last edited by DataLife on January 19th, 2012, 1:59 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 18th, 2012, 11:11 pm 
Sad but true. You'll need to run FtpPutFile in a separate real thread in order for AHK do do other stuff during the upload.

There are other ways to do FTP without bogging up a script, some even allow you to track the transfer progress, but they also require a lot of work.

I think this is a case of "pick your poison".


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: [VxE], BrandonHotkey, Edd, HotkeyStick, robotkoer and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group