threaded windows
threaded windows
I seem to be unable to grasp the concept how to start and end threads correctly. If I start a window thread inside the main loop via menu or button, I end up with multiple threads started at once. What is the right way to have a main program window and be able to start and close several identical threaded windows? If I put a few CreateThread commands before the main loop, they all start and work correctly. Please, help.
Re: threaded windows
WHy not use a thread handle ie:
define hndThread1 = 0
define hndThread2 = 0
define hndThread3 = 0
repeat
if hndThread1 = 0 : hndThread1 = CreateThread(@Thread1(),0) : endif
if hndThread2 = 0 : hndThread2 = CreateThread(@Thread2(),0) : endif
if hndThread3 = 0 : hndThread3 = CreateThread(@Thread3(),0) : endif
until bSomeCondition
define hndThread1 = 0
define hndThread2 = 0
define hndThread3 = 0
repeat
if hndThread1 = 0 : hndThread1 = CreateThread(@Thread1(),0) : endif
if hndThread2 = 0 : hndThread2 = CreateThread(@Thread2(),0) : endif
if hndThread3 = 0 : hndThread3 = CreateThread(@Thread3(),0) : endif
until bSomeCondition
Re: threaded windows
Not quite what I'm trying to achieve. Simply speaking I want just to be able to push button- start a thread. Push again- start another copy of identical thread. Then close them manually when they are not needed anymore. But as I put CreateThread under any gadget which can perform the command, the threads start in multiple, sometimes even without pressing the button. Obviously, I don't understand something crucial here.
Re: threaded windows
It is my opinion that if you have a window outside the main thread you're doing it wrong. It is possible, but it is totally unnecessary and only gives you problems like this.
Run all your windows normally. Instead put whatever long operations that might cause GUI slowness in a thread, so it doesn't slow the GUI down.
Run all your windows normally. Instead put whatever long operations that might cause GUI slowness in a thread, so it doesn't slow the GUI down.
Re: threaded windows
You have bug in your event loop if this is the case. Only check EventGadget() if WaitWindowEvent() returned #PB_Event_Gadget, else it's not a gadget event and EventGadget() is invalid!Not quite what I'm trying to achieve. Simply speaking I want just to be able to push button- start a thread. Push again- start another copy of identical thread. Then close them manually when they are not needed anymore. But as I put CreateThread under any gadget which can perform the command, the threads start in multiple, sometimes even without pressing the button. Obviously, I don't understand something crucial here.
Re: threaded windows
Well, just check if the thread was already started:
Code: Select all
Global ThreadActive
Procedure Thread(Void)
ThreadActive = #True
; ...
ThreadActive = #False
EndProcedure
If ThreadActive = #False
CreateThread(@Thread(), 0)
EndIf
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Re: threaded windows
Precisely that's the case. Thanks a lot.You have bug in your event loop if this is the case. Only check EventGadget() if WaitWindowEvent() returned #PB_Event_Gadget, else it's not a gadget event and EventGadget() is invalid!