Page 1 of 1

Work order for Bind-Commands

Posted: Fri Oct 08, 2021 3:29 am
by jacdelad
Hi,
are calls of functions linked via Bind... processed serially or simultaneously like threads?

Code: Select all

Global NewList MyList.i()

Procedure Processor()
  SelectElement(MyList(),20)
  Delay(3000)
EndProcedure

For counter=1 To 100
  AddElement(MyList())
  MyList()=counter
Next
OpenWindow(0,0,0,300,200,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ButtonGadget(1,0,0,300,200,"Push me!")
BindGadgetEvent(1,@Processor(),#PB_EventType_LeftClick)
SelectElement(MyList(),10)
Repeat
  Debug MyList()
  SelectElement(MyList(),10)
Until WaitWindowEvent(1000)=#PB_Event_CloseWindow
I hope this example explains it: Pushing the button causes to call the function which selects a different element and waits for 3 seconds (to be sure the WaitWindowEvent() is triggered at least three times). When I execute it the WaitWindowEvent() is not triggered when I pushed the button, element 21 is debugged once and then it goes on like normal. -> I conclude, that the program processes events triggered with BindGadgetEvent() and such serially. Is that correct and always the case (threads aside)?

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 3:46 pm
by mk-soft
Procedures connected to BindEvent or BindGadgetEvent are processed internally when WaitWindowEvent is called, so these procedures run in the main programme (GUI).
Therefore, no delay may be used, as otherwise the entire GUI will hang.

If an event comes from the OS (WaitWindowEvent waits for it), the connected procedures are first called before WaitWindowEvent passes the event to the main loop.

Code: Select all

;-TOP

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
  #Button
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration


Procedure DoEventButton()
  Debug "Process Button Click Over BindGadgetEvent"
EndProcedure

Procedure Main()
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
    ButtonGadget(#Button, 10, 10, 120, 25, "ClickMe!")
    
    BindGadgetEvent(#Button, @DoEventButton(), #PB_EventType_LeftClick)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #Button
              Debug "Process Button Click Over Main Event Loop"
          EndSelect
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 4:55 pm
by jacdelad
Ah, yes, that's what I wanted to hear. I only used the delay to test whether other events are handled simultaneously or not. So, without WaitWindowEvent all Binds won't work?

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 5:14 pm
by mk-soft
That right,

without WaitWindowEvent, nothing bind's work!

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 5:39 pm
by STARGÅTE
As an important info I have to say, that the call order of multiple bound events is from the last to the first.

Code: Select all

Enumeration
	#Window
	#Button
EndEnumeration

Procedure Event1()
  Debug "Event1 called"
EndProcedure

Procedure Event2()
  Debug "Event2 called"
EndProcedure

Procedure Event3()
  Debug "Event3 called"
EndProcedure

If OpenWindow(#Window, 0, 0, 140, 45, "Window", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	ButtonGadget(#Button, 10, 10, 120, 25, "ClickMe!")
	
	BindGadgetEvent(#Button, @Event1(), #PB_EventType_LeftClick)
	BindGadgetEvent(#Button, @Event2(), #PB_EventType_LeftClick)
	BindGadgetEvent(#Button, @Event3(), #PB_EventType_LeftClick)
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				Break
		EndSelect
	ForEver
	
EndIf
Event3 called
Event2 called
Event1 called

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 6:03 pm
by mk-soft
Hello STARGÅTE,

Even though your example is very nice, it bothers me a little that you put Window objects and Gadget objects in the same enumeration. Otherwise the new users will think that this is correct, even if this works.

Re: Work order for Bind-Commands

Posted: Fri Oct 08, 2021 6:39 pm
by jacdelad
@STARGÅTE: thanks for the annotation, that's good to know (and a bit odd imo...).