Page 1 of 1

waitprogram and programrunning

Posted: Sat Mar 30, 2019 9:55 pm
by khalidel
hey programmers, i run a program with runprogram and i want to show a waiting message like "please wait) because waitprogram freezes the system and i tried with while but still blocking.

Code: Select all

p = RunProgram("prog.exe", "/?", "", #PB_program_open|#pb_program_hide)
if p
while programrunning(p) 
setgadgettext(0, "fusion en cours...")
wend
 endif
i want that message to appear during the execution time of p program
thank you

Re: waitprogram and programrunning

Posted: Sat Mar 30, 2019 11:17 pm
by infratec
Hi,

for things like that you need a thread.

Re: waitprogram and programrunning

Posted: Sat Mar 30, 2019 11:42 pm
by infratec

Code: Select all

EnableExplicit

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Enable ThreadSave"
CompilerEndIf

Enumeration #PB_Event_FirstCustomValue
  #ThreadEvent
EndEnumeration


Structure ThreadStructure
  Thread.i
  Semaphore.i
  Programm.i
  Exit.i
EndStructure



Procedure WaitForProgrammEnd(*Thread.ThreadStructure)
  
  Protected i.i
  
  While ProgramRunning(*Thread\Programm) And Not *Thread\Exit
    i + 1
    PostEvent(#ThreadEvent, 0, 0, 0, i)
    WaitSemaphore(*Thread\Semaphore)
    Delay(100)
  Wend
  If *Thread\Exit
    KillProgram(*Thread\Programm)
  EndIf
  CloseProgram(*Thread\Programm)
  
EndProcedure


Define Event.i, Thread.ThreadStructure

OpenWindow(0, 0, 0, 100, 50, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

TextGadget(0, 10, 10, 200, 20, "")

Thread\Semaphore = CreateSemaphore()

Thread\Programm = RunProgram("notepad", "", "", #PB_Program_Open)
If Thread\Programm
  Thread\Thread = CreateThread(@WaitForProgrammEnd(), @Thread)
EndIf

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #ThreadEvent
      SetGadgetText(0, Str(EventData()))
      SignalSemaphore(Thread\Semaphore)
  EndSelect
Until Event = #PB_Event_CloseWindow

If IsThread(Thread\Thread)
  Thread\Exit = #True
  SignalSemaphore(Thread\Semaphore)
  If WaitThread(Thread\Thread, 3000) = 0
    KillThread(Thread\Thread)
  EndIf
EndIf

FreeSemaphore(Thread\Semaphore)

Re: waitprogram and programrunning

Posted: Sun Mar 31, 2019 4:21 pm
by khalidel
very nice. thank you very much. i adapt this code to mine with some changes and it works fine. i change delay(100) to delay(1000) and i use the procedure to count elapsed time, then i add some calculations to case thread to get minutes and seconds.

Re: waitprogram and programrunning

Posted: Sun Feb 14, 2021 3:11 pm
by Simo_na
infratec wrote:

Code: Select all

EnableExplicit.....
...and thank goodness it's 'basic', imagine if it wasn't 'basic' ... :-D