Page 1 of 1
Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 2:26 pm
by jassing
why is this locking up?
Code: Select all
Procedure something(void)
Debug "About to resize"
ResizeWindow(0,490,319,#PB_Ignore,#PB_Ignore)
Debug "Resized 1"
EndProcedure
OpenWindow(0, 10, 10, 100, 20, "")
h=CreateThread( @something(), 0 )
;something(0) ; this works, so it's not that there's no event loop yet....
WaitThread(h)
Debug "done"
Re: Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 4:36 pm
by spikey
Updating UI components from threads is not supported (but I can't find the original citation at the moment). It sometimes works but sometimes crashes on Windows and some Linux flavours, I understand it doesn't work on Macs.
You can however use PostEvent to post a custom event back from a thread back to the main window and have it update its own dimensions. This will work properly on all OS because the call to ResizeWindow will be on the main thread. (You may need proper mutex protection in practice, I've been lazy).
If you've got more sophisticated updates then you might like to check out the module that mk-soft wrote at:
https://www.purebasic.fr/english/viewto ... 12&t=66180
Code: Select all
#Custom_Event_ResizeNeeded = #PB_Event_FirstCustomValue
Global Size.SIZE
Procedure something(void)
Delay(5000)
Debug "About to resize"
Size\cx = 490
Size\cy = 319
PostEvent(#Custom_Event_ResizeNeeded, 0, 0)
Debug "Resized 1"
EndProcedure
OpenWindow(0, 10, 10, 100, 20, "")
h=CreateThread( @something(), 0 )
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit = 1
Case #Custom_Event_ResizeNeeded
ResizeWindow(0, Size\cx, Size\cy, #PB_Ignore,#PB_Ignore)
EndSelect
Until Quit = 1
Re: Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 5:47 pm
by jassing
spikey wrote: Fri May 24, 2024 4:36 pm
Updating UI components from threads is not supported (but I can't find the original citation at the moment). It sometimes works but sometimes crashes on Windows and some Linux flavours, I understand it doesn't work on Macs.
I thought that was for the events, not for updating windows/gadgets.
Anyway, I would be more understanding of an error (IMA, or something, not a hang).
This does work:
Code: Select all
Procedure something(void)
Debug "About to resize"
ResizeWindow(0,490,319,#PB_Ignore,#PB_Ignore)
Debug "Resized 1"
EndProcedure
OpenWindow(0, 10, 10, 100, 20, "")
h=CreateThread( @something(), 0 )
Repeat : WaitWindowEvent(0) : Until IsThread(h)=#False
Debug "done"
But your saying I can't rely on this, so nothing can be updated in a window from a thread.
spikey wrote: Fri May 24, 2024 4:36 pm
If you've got more sophisticated updates then you might like to check out the module that mk-soft wrote at:
I'll have a look, thank you.
Re: Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 6:34 pm
by fryquez
ResizeWindow() does send messages to the owning (creator of the window) thread,
but you are blocking it with WaitThread().
Avoid blocking function in the main GUI thread and let it process WindowEvent() calls.
Re: Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 6:39 pm
by jassing
fryquez wrote: Fri May 24, 2024 6:34 pm
ResizeWindow() does send messages to the owning (creator of the window) thread,
but you are blocking it with WaitThread().
Avoid blocking function in the main GUI thread and let it process WindowEvent() calls.
In my original code, I didn't have a waitthread() - it was a thread that was called to restore some defaults, and re-position the window.
so the main code was still running, as was the thread, the thread locked up on ResizeWindow()...
it's ok, I've gotten around it.
Just seems odd, if it's not in a thread, and it "sends a message", there still isn't anything to receive that message, and it doesn't lock then.
Re: Lock on resizewindow() with thread.
Posted: Fri May 24, 2024 8:59 pm
by spikey
jassing wrote: Fri May 24, 2024 5:47 pm
But your saying I can't rely on this, so nothing can be updated in a window from a thread.
Not directly and reliably, no.
jassing wrote: Fri May 24, 2024 6:39 pm
Just seems odd, if it's not in a thread, and it "sends a message", there still isn't anything to receive that message, and it doesn't lock then.
I should have more precisely used the term 'deadlock'. In this case it's the same thread that receives the message (because there's a queue). A single thread can't deadlock because its execution point will be deterministic. It isn't guaranteed to deadlock every time, only if both threads are blocked on each other. It's only a problem in the not entirely deterministic realm of threaded programming.