Page 1 of 1

Thread waits end main process ?

Posted: Wed Nov 26, 2008 4:35 pm
by erhk
Hi,

It seems, main process and thread process don't run asynchronously !

Regards

Code: Select all

Enumeration 
  #Win0
  #Btn_Run              : #Btn_Exit
  #Compteur_Thread      : #Compteur_Win0
EndEnumeration 

Global Compteur_Thread

Procedure Add(prm)

  For Compteur_Thread = 0 To 9999
    SetGadgetText(#Compteur_Thread, Str(Compteur_Thread))
  Next
  
EndProcedure 

If OpenWindow(#Win0, 400, 250, 180, 160, "Fenetre 0", #PB_Window_TitleBar)

  If CreateGadgetList(WindowID(#Win0))
    TextGadget(#PB_Any, 20, 20, 100, 20, "Compteur Thread :")
    TextGadget(#Compteur_Thread, 120, 20, 50, 20, Str(Compteur_Thread))
    TextGadget(#PB_Any, 20, 50, 100, 20, "Compteur Win0    :")
    TextGadget(#Compteur_Win0, 120, 50, 50, 20, Str(Compteur_Win0))
    ButtonGadget(#Btn_Run,    10, 130, 70, 20, "Run") 
    ButtonGadget(#Btn_Exit, 100, 130, 70, 20, "Exit")
  EndIf 

  Repeat 
    Event = WaitWindowEvent()
      
      If Event =  #PB_Event_Gadget 
        Select EventGadget()
        
          Case #Btn_Run
          
            ThreadID = CreateThread(@Add(), 1)
            ; ===> If ThreadID : WaitThread(ThreadID) : EndIf ; IF ACTIVED = FREEZ !
            
            For Compteur_Win0 = 0 To 29999
              SetGadgetText(#Compteur_Win0, Str(Compteur_Win0))
            Next
            
          Case #Btn_Exit
            Event = #PB_Event_CloseWindow  
        EndSelect 
      EndIf 

  Until Event = #PB_Event_CloseWindow 
EndIf

Posted: Wed Nov 26, 2008 5:49 pm
by Demivec
erhk wrote:It seems, main process and thread process don't run asynchronously !
They do. What is happening in your example code is that you aren't processing the window events each time the SetGadgetText() function is executted . You change the display of a gadget so it would need to be redrawn via a window event. When you finally do process a window event it is after the thread and the main process code are done looping through the changes to the gadget, that's when you see the result of all the events that would have updated the gadgets display.

Try changing your example slightly to process window events during the loop in the main code (starting on line#39), like this:

Code: Select all

          For Compteur_Win0 = 0 To 29999
            SetGadgetText(#Compteur_Win0, Str(Compteur_Win0))
            Repeat:event = WindowEvent():Until event = 0  ;<==not perfect, but demonstrates what I mean.
          Next

Posted: Wed Nov 26, 2008 7:02 pm
by freak
SetGadgetText() (from the thread) requires thread synchronisation to deliver the window messages. So you need to call (Wait)WindowEvent() in the main thread if you modify gadgets from a thread.

You can do this by using WaitThread() with a small timeout and calling (Wait)WindowEvent() inbetween to handle events.

Posted: Wed Nov 26, 2008 7:42 pm
by erhk
Thanks for your help and explanations. :D
It's running, and I have understood (I think :wink: )