PostEvent in Thread

Just starting out? Need help? Post your questions and find answers here.
tatanas
Enthusiast
Enthusiast
Posts: 199
Joined: Wed Nov 06, 2019 10:28 am
Location: France

PostEvent in Thread

Post by tatanas »

Hi,

I have a program with a big event loop for my main window, so I decided to create a second loop to handle specific code of a second window. I know it's not perfect...
So here is the schema of what I'm trying to do and where is my problem :

MAIN EVENT LOOP (first GUI)
|--> start 2nd Event Loop within a THREAD (second GUI)
|--> this thread start a new worker thread
|--> the worker thread PostEvent() to the second GUI (FAILED !)

The Event is received by the main event loop. :cry:
Is there a way to receive the event in the second gui ?

Thanks for your time.


Sample :

Code: Select all

Enumeration #PB_Event_FirstCustomValue
	#customevent
EndEnumeration

; WORKER THREAD
Procedure WorkerThread(*value)
	Debug "processing..."
	Delay(2000)
	PostEvent(#customevent, 1, 1)
EndProcedure


; SECOND EVENT LOOP (Threaded)
Procedure NewWINThreaded(*value)
	DisableDebugger
	OpenWindow(1, 100, 100, 300, 100, "Win threaded", #PB_Window_SizeGadget | #PB_Window_SystemMenu | #PB_Window_MaximizeGadget)
	ButtonGadget(1, 10, 10, 100, 30, "Create Thread")
	EnableDebugger

	Repeat
		DisableDebugger
		event = WaitWindowEvent()
		EnableDebugger

		If event = #PB_Event_Gadget
			If EventGadget() = 1
				Debug "WorkerThread"
				CreateThread(@WorkerThread(), 0)
			EndIf
		EndIf
		If event = #customevent
			Debug "#customevent received in Threaded Loop"
		EndIf
		
	Until event = #PB_Event_CloseWindow
EndProcedure



; MAIN EVENT LOOP

OpenWindow(0, 100, 100, 300, 100, "Main", #PB_Window_SizeGadget | #PB_Window_SystemMenu | #PB_Window_MaximizeGadget)
ButtonGadget(0, 10, 10, 100, 30, "Push me")

Repeat

	event = WaitWindowEvent()
	If event = #PB_Event_Gadget
		If EventGadget() = 0
			Debug "NewWINThreaded"
			CreateThread(@NewWINThreaded(), 1)
		EndIf
	EndIf
	If event = #customevent
		Debug "#customevent received in Main Loop"
	EndIf

Until event = #PB_Event_CloseWindow
Windows 10 Pro x64
PureBasic 6.04 x64
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: PostEvent in Thread

Post by NicTheQuick »

You can only have one event loop. I you react to multiple (Wait)WindowEvent() in different threads you never know which one gets the events you need.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PostEvent in Thread

Post by infratec »

As already mentioned: one event loop.

Look at:

Code: Select all

EventWindow()
And read also about OpenWindow().
There is written: OpenWindow() should not be opened in a thread.
It is also better to follow this in Windos.
tatanas
Enthusiast
Enthusiast
Posts: 199
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: PostEvent in Thread

Post by tatanas »

I read a lot on this forum about multiple event loop (especially inside threads) and i thought it was more a problem of compatibility with MacOS/Linux than a "bug" in Windows. That's why there is a Debugger error. And as you can see the events from gadget are well triggered and received.
This is PostEvent() only.
But if there is no other way, I will overload the main loop, it was just to be more readable...
Windows 10 Pro x64
PureBasic 6.04 x64
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PostEvent in Thread

Post by infratec »

It has nothing to do with th OS.

If you have more then one event loop, you can always loose events for the other one.
That's not good, since these are not only PB events.
Post Reply