waitprogram and programrunning

Just starting out? Need help? Post your questions and find answers here.
khalidel
User
User
Posts: 17
Joined: Sat Mar 23, 2019 7:45 pm

waitprogram and programrunning

Post 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
infratec
Always Here
Always Here
Posts: 7584
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: waitprogram and programrunning

Post by infratec »

Hi,

for things like that you need a thread.
infratec
Always Here
Always Here
Posts: 7584
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: waitprogram and programrunning

Post 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)
khalidel
User
User
Posts: 17
Joined: Sat Mar 23, 2019 7:45 pm

Re: waitprogram and programrunning

Post 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.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: waitprogram and programrunning

Post by Simo_na »

infratec wrote:

Code: Select all

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