Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto to execute a function in a separate thread

Status
Not open for further replies.

mibosoft

Programmer
Jul 23, 2002
106
SE
Hi,
My app has built-in FTP functionaly available as a procedure in a .prg file. I would like to execute this procedure without having to wait for it to finalize like "do ftp nowait". Is there a way to achieve this.

I thought of putting it in own .dll but then (apparently) I can't use dialogs and putting it in an own .exe probably gets slow (.exe startup).

/Micael
 
Thanks Borislav,
Perhaps this works. It seems however that the task cannot report status during execution like "Sending file 20 of 50" and this is something you want for lengthy tasks. Another thing is I don't want to rely on something that I don't have source code for. It might work with VFP9 but not VFP15.

/Micael
 
I hope we will have VFP15 :)
That DLL is writen on C++, so I doubt it will stops working after a few years.
Other way is to build your own FLL that will do the same.
Becuase the VFP is not multithread language (I hope Sedna will allow us to have different threads but who knows)

Borislav Borissov
 

Micael,

How are you handling the FTP side of things? Usually, FTP is asynchronous, so you don't have to worry about running it in a separate thread.

For example, if you use the Microsoft Internet Transfer ActiveX control, just call its Execute() method, passing the FTP command you want to send. Your program will regain control immediately, even if the transfer takes a long time. You can use the StillExecuting property to find out when the transfer has finished.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Hi Mike,
My ftp is based on a piece of code that I found in the Tek-Tips FAQ, faq184-3234, "How do I transfer files using FTP?". Here is the send file part:

DECLARE INTEGER FtpPutFile IN wininet.DLL;
INTEGER hConnect,;
STRING lpszLocalFile,;
STRING lpszNewRemoteFile,;
INTEGER dwFlags,;
INTEGER dwContext

PUBLIC hOpen, hftpSession

lcHost = ALLTRIM(lcHost)
lcUser = ALLTRIM(lcUser)
lcPassword = ALLTRIM(lcPassword)
lcSource = ALLTRIM(lcSource)
lcTarget = ALLTRIM(lcTarget)

IF connect2ftp (lcHost, lcUser, lcPassword)
WAIT WINDOW 'Transferring....' NOWAIT
IF FtpPutFile(hftpSession, lcSource,;
lcTarget, lnXFerType, 0) = 1
WAIT WINDOW lcSource + ' transferred.' TIMEOUT 2
ENDIF

= InternetCloseHandle (hftpSession)
= InternetCloseHandle (hOpen)
ENDIF

I dont't think that FtpPutFile() can handle wildcards so I put a for loop around it to send a bunch of files (not in the example above). So even if FtpPutFile() is asynchronous I guess my loop destroys it.

I will try the ActiveX control you mentioned.

Thanks,
Micael
 

Micael,

I'm not sure if the ActiveX control is better than the way you did it, but I do know that it can be made to run asynchronously.

If you do try it, come back if you have any specific questions on it.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Mike,
Can the ActiveX handle several files in one command (like array of files or *.ext)? Do you happen to have a code example?

I read somewhere that the ActiveX is a wrapper around the wininet.DLL I'm already using so maybe the limitations are the same.

/Micael
 

Micael,

Can the ActiveX handle several files in one command (like array of files or *.ext)?

The ActiveX control lets you send any FTP command, including commands with wildcards.

Assuming you have placed the control on a form, and named it (say) oleFTP, the following code should work:

Code:
lcCommand = "DIR *.TXT"
lcServer = "FTP://ftp.microsoft.com"
* Send the command
THISFORM.oleFTP.Execute(lcServer, lcCommand)
DO WHILE THISFORM.oleFTP.StillExecuting
  * Do anything you like here
ENDDO
* Command has now finished, so get the result
lcResult = THISFORM.oleFTP.GetChunk(64000)

I haven't tested that particular code, but it is similar to production code I have used elsewhere.

By the way, the 64000 passed to GetChunk is just a high number that determines how many bytes to retrieve from the buffer in one go.

Hope this is of use.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
If you are just sending FTP commands PUT for sending 1 file
MPUT for sending many and GET for receiving 1 file and MGET for receiving many ie PUT myfile1.txt and MPUT myfile?.*


Steve Bowman
steve@independenttech.net
Independent Technology, Inc.
CA, USA
 
Thanks for the input,
I tried the following with "Protocol" set to 2 (FTP):

***********
lcCommand = "GET index.html C:\temp\index.html"
lcServer = "FTP://ftp.fyrislundsbk.com"

*Probably not needed
THISFORM.OleInetControl.remoteHost="FTP://ftp.fyrislundsbk.com"

THISFORM.OleInetControl.remotePort=21
THISFORM.OleInetControl.userName="fyrislundsbk"
THISFORM.OleInetControl.password="my_pw"

THISFORM.OleInetControl.Execute(lcServer,lcCommand)
DO WHILE THISFORM.OleInetControl.StillExecuting
wait window "sending" nowait
* Do anything you like here
ENDDO
* Command has now finished, so get the result
lcResult = THISFORM.OleInetControl.GetChunk(64000)
wait window "done" nowait
***********

I get the OLE error message "Unable to connect to remote host" even though the address and user info is correct.

If I set "Protocol" to "default", I get a connection but then I can't FTP.

/Micael
 
Hi Dave,
Did I swap source and destination? Tested the other way around without success. If I use:
GET index.html index.html
I should get the file into the directory where my .prg is but nope :-(
(I use the default protocol now).
/Micael
 
I guess that's what I was saying - if the index.html file is in the root folder (ftproot or \), you should be able to use GET index.html index.html. But if the index.html is on your hard drive as c:\temp\index.html, you would need to have c:\temp added as a virtual folder on the host, so that when you log in it is visible.
Can you see the file with any other ftp client?


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Now I am confused

GET sourcedir/file "/" if target ftp is a unix (space) destination

GET /index.html c:\temp\index.html would retrieve from remote system to local system

If it was to go to (based on systax) c:\temp
that would be a PUT not a GET

PUT index.html c:\temp\index.html

for both PUT and GET syntax is

GET/PUT sourcedir/file (space) destinationdir/file

Sorry if this is elementary for anyone but all the ftp root talk was getting me confused on the direction needed

Steve Bowman
steve@independenttech.net
Independent Technology, Inc.
CA, USA
 
I use the correct src/dest (and unix slashes). No error messages are given and still no file is transferred, neither direction.

Anyway, thanks for all help. I think I will stick to my solution (above) working directly with the wininet.DLL. It works fine except that it doesn't execute in an own thread.

/Micael
 
Micael said:
Anyway, thanks for all help. I think I will stick to my solution (above) working directly with the wininet.DLL. It works fine except that it doesn't execute in an own thread.
Micael,

Why not wrap the wininet.DLL stuff up in a VFP COM exe and then call this from your application?

By default VPF COM exe's run out-of-process (READ: in their own process).

Andy

 

Andy,

By default VPF COM exe's run out-of-process

It's true they're out of process, but does that mean they are asynchronous? I thought all COM processes were synchronous (unless used with something like the Queued Components manager).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
From the VFP 9 help titled "Automation Servers in Visual Foxpro":

After you create an Automation server, you can build it as an out-of-process or an in-process component. An out-of-process component is an executable (.exe) file that runs in its own process. An in-process component is a dynamic-link library (.dll) file that runs in the same process address space as the client that calls it. Communication between a client application and an out-of-process server is called cross-process communication.

It's true too. If you fire up a VFP COM exe from within another VFP app you can look at the Windows Task Manager and see both processes running individually.

Andy

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top