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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitor Size of text file as created

Status
Not open for further replies.

AlastairP

Technical User
Feb 8, 2011
286
AU
I am using shellexecute to run a command and using '>' to output the displayed text to a text file.
The problem I have is that the file is missing text as my program continues on to open the file and parse the text.
How can I get my program to pause until shellexecute is finished?
I can declare a sleep which does work after a fashion but I have to estimate the longest time it will take.
It would be better if I could monitor the file size with a Do While function until the file size has stopped increasing.

Any suggestions?

Alastair

 
I suggest the following sequence:
1. Create a file to indicate that the process is not finished, for instance NotFinished.txt. Strtofile() is fine for this
2. Run your ShellExecute routine
3. Delete the file mentioned in 1
3. Let the VFP program you refer to run in a loop until the file mentioned in 1 is not found anymore.
 
How can I get my program to pause until shellexecute is finished?

You can't. Once you have launched a program with ShellExecute(), that program exists independently of VFP. You can't control it in any way from your VFP application. The best you can do is to use API calls to look through all the open windows on the desktop, checking for one whose title matches that of the launched program. But it's not a very reliable method.

As an alternative, consider using this code in place of ShellExecute()

Code:
oWSH = CREATEOBJECT("wscript.shell")
oWSH.Run("MyProg", 3, .T.)

where "MyProg" is the program you want to execute, including the full path and EXE name. The second parameter says how you want the program window to appear (3 = maximised). Importantly, by passing .T. as the third parameter, you tell your calling program to pause until the called program has finished executing.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 

I came up with this:
Tested and seems to work.
I will look at your suggestion as well Mike.

Alastair

Code:
lnSize = -1
nSize =0

lcFileIsComplete=.f.
DO WHILE lcFileIsComplete=.f.
	DECLARE Sleep IN Win32API INTEGER nMilliseconds
	Sleep(10)
	lnFile=ADIR(arr,lcFile,"D",1)
	IF lnFile = 0 
		LOOP 
	ENDIF 	
	nSize =arr(1,2)
	IF nSize=0
		LOOP
	ENDIF 	
	IF nSize = lnSize 
		lcFileIsComplete=.t.
		EXIT 
	ENDIF 
	lnSize 	= nSize 
ENDDO

 
Mike, I tried your suggestion, but I had an ole error that it could not wait for process when I had the last parameter set to .t.

Anyway, that aside, does this wscript return a window handle so I can close the open window I just opened with the command?
FYI I am trying to activate a remote share on a server that once connected needs a kick start by typing in the ip address in an explorer bar.
 
Scott,

No, the script doesn't return a handle. As far as I can see, it always returns zero.

I suspect that your OLE error occurs because it does not recognise - or cannot find - the file or command that you passed to it. Although if ShellExecute() worked, then this should as well. Sorry I can't suggest anything else.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
By the way, Alastair, there is a small problem with the code that you posted. You really need to take the [tt]DECLARE Sleep[/tt] outside the [tt]DO WHILE[/tt] loop. You only need to do it once. Doing inside the loop will still work, but will lead to a (probably negligible) drop in performance.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,
Oh great, thanks for that.
Its short on a bit of validation too and could get stuck in a loop if the file is empty. I will run a counter and exit after a number of loops.

Alastair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top