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
I guess I am missing something quite straight forward here. Thanks for any help!
merendo