threaded windows

Just starting out? Need help? Post your questions and find answers here.
daft
User
User
Posts: 25
Joined: Fri Oct 29, 2010 10:32 am

threaded windows

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: threaded windows

Post 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
daft
User
User
Posts: 25
Joined: Fri Oct 29, 2010 10:32 am

Re: threaded windows

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: threaded windows

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: threaded windows

Post 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!
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: threaded windows

Post 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
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
daft
User
User
Posts: 25
Joined: Fri Oct 29, 2010 10:32 am

Re: threaded windows

Post 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.
Post Reply