Here is the correct way:
Code: Select all
Enumeration
#Form0
#EditorCommand
#StringCommand
#Button
EndEnumeration
IncludeFile "ExternalProgram.pbi"
Define Event.i, Exit.i, i.i, j.i
Define ThreadParameter.ExternalProgramParameterStructure
Define Source$, Destination$, ProgramFilePath$
ProgramFilePath$ = GetPathPart(ProgramFilename())
Source$ = PathRequester("Source", ProgramFilePath$)
If Source$
Destination$ = PathRequester("Destination", ProgramFilePath$)
If Destination$
LoadFont(0, "Consolas", 10)
OpenWindow(#Form0, 379, 176, 608, 542, "Backup User Profile", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_TitleBar)
EditorGadget(#EditorCommand, 5, 15, 593, 447, #PB_Editor_ReadOnly)
SetGadgetFont(#EditorCommand, FontID(0))
StringGadget(#StringCommand, 6, 469, 594, 23, "/S /E /TEE /log:backup.log "+ Source$ + " " + Destination$)
ButtonGadget(#Button, 211, 503, 201, 33, "Begin Backup!")
ThreadParameter\Program$ = "robocopy.exe"
ThreadParameter\ProgramReadWriteMode = #PB_Program_Ascii
ThreadParameter\ProgramExit$ = "Break"
ThreadParameter\Semaphore = CreateSemaphore()
ThreadParameter\Mutex = CreateMutex()
Repeat
Event = WaitWindowEvent()
Select Event
Case #ExternalProgram_Event_StdOut
ThreadParameter\StdOut$ = RemoveString(ThreadParameter\StdOut$, #CR$)
ThreadParameter\StdOut$ = RTrim(ThreadParameter\StdOut$, #LF$)
i = CountString(ThreadParameter\StdOut$, #LF$)
For j = 0 To i
AddGadgetItem(#EditorCommand, -1, StringField(ThreadParameter\StdOut$, j + 1, #LF$))
Next j
SignalSemaphore(ThreadParameter\Semaphore)
Case #ExternalProgram_Event_Error
Debug ThreadParameter\StdErr$
SignalSemaphore(ThreadParameter\Semaphore)
Case #ExternalProgram_Event_Exit
DisableGadget(#Button, #False)
Case #PB_Event_Gadget
Select EventGadget()
Case #Button
ThreadParameter\ProgramParameter$ = GetGadgetText(#StringCommand)
ThreadParameter\Thread = CreateThread(@ExternalProgramThread(), @ThreadParameter)
DisableGadget(#Button, #True)
EndSelect
Case #PB_Event_CloseWindow
If IsThread(ThreadParameter\Thread)
ThreadParameter\Exit = #True
WaitThread(ThreadParameter\Thread)
EndIf
FreeMutex(ThreadParameter\Mutex)
FreeSemaphore(ThreadParameter\Semaphore)
Exit = #True
EndSelect
Until Exit
EndIf
EndIf
The character modes were not correct handled.