ReadProgramData() stalls my application - why?

Just starting out? Need help? Post your questions and find answers here.
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

ReadProgramData() stalls my application - why?

Post by merendo »

Cheers folks!

Please take a look at this tiny little code example. It basically sums up how I would like to use Robocopy from the application I am writing. I would also like to read back whatever robocopy outputs. Problem is: Whenever I use ReadProgramData(), it stalls my application (even though I always check if there is any data to be read), possibly for quite some time, which is not acceptable in my application as it has to deal with plenty of other stuff, too. I realise I could use threads but I'd really prefer to steer clear of those since they have to date always given me a headache whenever I tried to use them.

Code: Select all

parameter$ = "*.* /E /R:1 /W:1 /IT /NP /NS /NC /V /IPG:500" ; Just some standard parameters to parse robocopy. IPG:500 is intentional to emulate a slow network.

OpenConsole()

PrintN("Please enter RC source path: ")
source$ = RTrim(Input(), "\")
PrintN("")
PrintN("Please enter RC target path: ")
target$ = RTrim(Input(), "\")

If Not FileSize(source$) = -2 Or Not FileSize(target$) = -2 : End : EndIf ; Terminate the programme if an invalid directory has been supplied

parameter$ = Chr(34)+source$+Chr(34)+" "+Chr(34)+target$+Chr(34)+" "+parameter$

Program = RunProgram("robocopy", parameter$, "", #PB_Program_Open|#PB_Program_Read) ; Start robocopy

If Not Program
	PrintN("Oops... Something went wrong. Aborting!")
	End
EndIf

PrintN("Robocopy started")

*RC_Readbuffer = AllocateMemory(2048) ; Memory buffer to write the ReadProgramData() to
previoustimestamp = ElapsedMilliseconds() ; Used to evaluate for how long the loop has been stalled

Repeat ; Main loop
	
	If Not ProgramRunning(Program) ; Check whether or not the programme execution has completed.
	 	PrintN("Program execution has finished.")
	 	Input()
	 	End
	EndIf
	
	If AvailableProgramOutput(Program) ; Check if program data is available
		If ReadProgramData(Program, *RC_Readbuffer, 2048) ; Then read that...
			PrintN(PeekS(*RC_Readbuffer)) ;..., then peek it and print it to the console.
		EndIf
	EndIf
	
	now = ElapsedMilliseconds() - previoustimestamp
	If Not now = 0
		ConsoleColor(11, 0)
		PrintN(Str(now) + "ms since previous loop") ; Print for how long the loop has been stalled.
		ConsoleColor(7, 0)
	EndIf
	previoustimestamp = ElapsedMilliseconds()
	
ForEver
Please supply two valid and read/write-accessible paths to the snippet, robocopy will copy all the files in the source$ to the target$. The IPG (inter packet gap) of 500ms is intentional, I use it to emulate a slow network as this is where my application will be running. The snippet will, when it detects stalling, print for how long it has been stalled.

I guess I am missing something quite straight forward here. Thanks for any help!

merendo
The truth is never confined to a single number - especially scientific truth!
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ReadProgramData() stalls my application - why?

Post by Fred »

ReadProgramData() is a blocking call until it reads all the required size. You should use the result of AvailableProgramOutput() to get the available size and use it in ReadProgramData()
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: ReadProgramData() stalls my application - why?

Post by merendo »

Thanks for your response! I updated my code to check whether or not data is to be read, but I get some weird output to the console. There seem to be plenty of empty line breaks. Any idea why that is?

Code: Select all

parameter$ = "*.* /E /R:1 /W:1 /IT /NP /NS /NC /V" ; Just some standard parameters to parse robocopy. IPG:500 is intentional to emulate a slow network.

OpenConsole()

PrintN("Please enter RC source path: ")
source$ = RTrim(Input(), "\")
PrintN("")
PrintN("Please enter RC target path: ")
target$ = RTrim(Input(), "\")

If Not FileSize(source$) = -2 Or Not FileSize(target$) = -2 : End : EndIf ; Terminate the programme if an invalid directory has been supplied

parameter$ = Chr(34)+source$+Chr(34)+" "+Chr(34)+target$+Chr(34)+" "+parameter$

Program = RunProgram("robocopy", parameter$, "", #PB_Program_Open|#PB_Program_Read) ; Start robocopy

If Not Program
   PrintN("Oops... Something went wrong. Aborting!")
   End
EndIf

PrintN("Robocopy started")

*RC_Readbuffer = AllocateMemory(2048) ; Memory buffer to write the ReadProgramData() to
previoustimestamp = ElapsedMilliseconds() ; Used to evaluate for how long the loop has been stalled

Repeat ; Main loop
   
  If Not ProgramRunning(Program) ; Check whether or not the programme execution has completed.
      PrintN("Program execution has finished.")
      Input()
      End
  EndIf
   
  ProgramOutput = AvailableProgramOutput(Program) ; Check if program data is available
  If ProgramOutput
	  If ReadProgramData(Program, *RC_Readbuffer, ProgramOutput) ; Then read that...
	  	PrintN(PeekS(*RC_Readbuffer, ProgramOutput, #PB_Ascii)) ;..., then peek it and print it to the console
	  EndIf
  EndIf
   
  now = ElapsedMilliseconds() - previoustimestamp
  If Not now = 0
    ConsoleColor(11, 0)
    PrintN(Str(now) + "ms since previous loop") ; Print for how long the loop has been stalled.
    ConsoleColor(7, 0)
  EndIf
  previoustimestamp = ElapsedMilliseconds()
   
ForEver
The truth is never confined to a single number - especially scientific truth!
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ReadProgramData() stalls my application - why?

Post by infratec »

Hi,

use this:

Code: Select all

If ReadProgramData(Program, *RC_Readbuffer, ProgramOutput) ; Then read that...
  Print(PeekS(*RC_Readbuffer, ProgramOutput, #PB_Ascii)) ;..., then peek it and print it to the console
EndIf
Because the CR is already at the end of the output of robocopy.

Bernd
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: ReadProgramData() stalls my application - why?

Post by merendo »

Okay, this is weird... It works now, previously it gave me some horribly aligned output. Thanks a lot, it works now!
The truth is never confined to a single number - especially scientific truth!
Post Reply