Page 1 of 1

threaded windows

Posted: Tue Jan 18, 2011 8:37 am
by daft
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

Posted: Tue Jan 18, 2011 9:25 am
by jassing
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

Re: threaded windows

Posted: Tue Jan 18, 2011 10:20 am
by daft
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

Posted: Tue Jan 18, 2011 12:03 pm
by Trond
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.

Re: threaded windows

Posted: Tue Jan 18, 2011 12:05 pm
by Trond
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.
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!

Re: threaded windows

Posted: Tue Jan 18, 2011 12:06 pm
by c4s
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

Re: threaded windows

Posted: Tue Jan 18, 2011 1:31 pm
by daft
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!
Precisely that's the case. Thanks a lot.