Page 1 of 1

WaitThread problem

Posted: Sat Jun 21, 2025 6:26 pm
by Sergey
Hi,
when I want to change listicon text in thread it becomes unlimited
Just comment line 3 and all will be ok, multithreading is enabled
PB6.21 final Win10 x64
Both codes WaitThread or While-Wend are freezing

Code: Select all

Procedure thread(thread_param)
	Delay(1000)
	SetGadgetItemText(0, 0, "Wait")
	Delay(1000)
EndProcedure

Procedure.s start_thread()
	thread = CreateThread(@thread(), 0)
	If thread
		DisableGadget(1, 1)
		
		SetGadgetItemText(0, 0, "Start")
		Debug "start"
		
		WaitThread(thread)
		
; 		While IsThread(thread)
; 			Debug "tick"
; 			Delay(100)
; 		Wend
		
		SetGadgetItemText(0, 0, "Done")
		Debug "done"
		
		DisableGadget(1, 0)
	EndIf
EndProcedure


If OpenWindow(0, 100, 200, 200, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
	ListIconGadget(0, 5, 5, 190, 225, "Status", 150)
	AddGadgetItem(0, 0, "?")
	ButtonGadget(1, 5, 235, 190, 20, "Start thread")
		
	Repeat
		Event = WaitWindowEvent()
		
		Select Event
		Case #PB_Event_Gadget
			If EventGadget() = 1
				start_thread()
			EndIf
		Case #PB_Event_CloseWindow
			Quit = 1
		EndSelect
		
	Until Quit = 1
EndIf

Re: WaitThread problem

Posted: Sat Jun 21, 2025 6:51 pm
by jacdelad
1. You don't distinguish between the eventtypes, so, how many times will start_thread() be executed?
2. GUI changes are not meant to be done within threads. Use a custom message.
3. start_thread() is not started within its own thread, so the whole message handling is halted until the procedure is done -> the program freezes until then.

Re: WaitThread problem

Posted: Sat Jun 21, 2025 7:52 pm
by Sergey
Hi, jacdelad
This is a example, not working program

1) start_thread run once
2) how can I change listicon during the thread?
3) this is done specifically to show that the execution of WaitThread
does not allow to change the contents of the ListIconGadget

Code: Select all

Procedure thread(param)
	Delay(1000)
	SetGadgetItemText(0, 0, "Wait")
EndProcedure

Procedure.s start_thread()
	thread = CreateThread(@thread(), 0)
	If thread		
		SetGadgetItemText(0, 0, "Start")
		Debug "start"
		
		WaitThread(thread)
		
		SetGadgetItemText(0, 0, "Done")
		Debug "done"
	EndIf
EndProcedure

If OpenWindow(0, 100, 200, 200, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
	ListIconGadget(0, 5, 5, 190, 250, "Status", 150)
	AddGadgetItem(0, 0, "?")
	
	start_thread()
	
	Repeat
	Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: WaitThread problem

Posted: Sat Jun 21, 2025 8:45 pm
by Mijikai
Example:
One eventloop -> thread posts events.

Code: Select all

Enumeration
  #EVENT_START = #PB_Event_FirstCustomValue
  #EVENT_WAIT
  #EVENT_STOP
EndEnumeration

Procedure thread(param)
  PostEvent(#EVENT_START) : Delay(1000)
  PostEvent(#EVENT_WAIT)  : Delay(1000)
	PostEvent(#EVENT_STOP)
EndProcedure

If OpenWindow(0, 100, 200, 200, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  ListIconGadget(0, 5, 5, 190, 250, "Status", 150)
  AddGadgetItem(0, 0, "?")
  t = CreateThread(@thread(),#Null)
  Repeat
    Select WaitWindowEvent() 
      Case #EVENT_START
        SetGadgetItemText(0,0,"Start")
      Case #EVENT_WAIT
        SetGadgetItemText(0,0,"Wait")
      Case #EVENT_STOP
        SetGadgetItemText(0,0,"Stop")
      Case #PB_Event_CloseWindow
        If IsThread(t):WaitThread(t):EndIf
        Break
    EndSelect
  ForEver
EndIf

Re: WaitThread problem

Posted: Sat Jun 21, 2025 9:41 pm
by Sergey
Mijikai, very very nice! You're a genius! Thank you, now works fine.
I never used PostEvent and didn't know about its existence :)

Re: WaitThread problem

Posted: Sat Jun 21, 2025 10:53 pm
by jacdelad
That's basically what I said...

Re: WaitThread problem

Posted: Sun Jun 22, 2025 12:01 pm
by mk-soft
For more control of threads see

Link: Mini Thread Control

Better use Named Enumeration

Code: Select all

Enumeration CustomEvent #PB_Event_FirstCustomValue
  #EVENT_START
  #EVENT_WAIT
  #EVENT_STOP
EndEnumeration

; Next include ...

Enumeration CustomEvent
  #EVENT_Next
  ; ...
EndEnumeration

Re: WaitThread problem

Posted: Sun Jun 22, 2025 12:18 pm
by acreis
Tank you very much about custom enumeration trick!