WaitThread problem

Windows specific forum
Sergey
User
User
Posts: 53
Joined: Wed Jan 12, 2022 2:41 pm

WaitThread problem

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: WaitThread problem

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Sergey
User
User
Posts: 53
Joined: Wed Jan 12, 2022 2:41 pm

Re: WaitThread problem

Post 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
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: WaitThread problem

Post 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
Sergey
User
User
Posts: 53
Joined: Wed Jan 12, 2022 2:41 pm

Re: WaitThread problem

Post 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 :)
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: WaitThread problem

Post by jacdelad »

That's basically what I said...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WaitThread problem

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
acreis
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Jun 01, 2012 12:20 am

Re: WaitThread problem

Post by acreis »

Tank you very much about custom enumeration trick!
Post Reply