Raised Event = Totally New Stack?

Hey all,

In doing some debugging, I’ve just discovered something that I had not realized until today (maybe I should have but I had not)…

It appears that raising an event that does not return a value, creates a whole new stack. Meaning, you make the call to raise the event and your current method continues on after that. The event the takes place some times later. I always thought that order execution would be the call, the event method and then the code after the event call.

But in the code block below:

AddHandler me.ContainerInitializedLoadData, AddressOf VWCWin.LoadDataIntoContainer
AddHandler me.APICommandProcessed, AddressOf VWCWin.CandC_APICommandProcessed

RaiseEvent ContainerInitializedLoadData(ContainerIndex)

StartingUp = False

The StartingUp property gets set PRIOR to the ContainerInitializedLoadData event taking place. If I were to make that event a function that returns a value, then that would force the election to happen in order. But without that, it appears that raised events just take place “some time” after they are raised.

Is this correct behavior (I assume it is by design in Xojo)? Am I seeing this correctly too?

Thanks!

is that Web or Desktop?

RaiseEvent is normally directly and calls on same thread.

Desktop. The event is raised on the same thread (main). It’s just happening at some other point in time. It is not following sequentially.

I discovered it because there are some things in that event method that should only execute if the value of StartingUp is false, but yet they were happening. So I started adding break points to see what was going on. The breakpoint for “StartingUp = False” above was happening before the breakpoint in the event method.

For me it shows nicely in the debugger.
I see the stack trace.

That’s odd as for me it is a totally new stack.

The only reason this should be is if the event is raised asynchronously.

Just out of curiosity, where is this code where you raise the event?

So then why is it happening asynchronously?

And how would you raise it asynchronously? Via a timer?

[quote=319095:@Jon Ogden]So then why is it happening asynchronously?

And how would you raise it asynchronously? Via a timer?[/quote]
Timer, thread, socket. But again, which event/method are you raising from?

The code I posted is in a container control that gets embedded in a Window. The handler method for the event is in that Window. That’s in the AddHandler directive.

Should I be adding the handler from the containing Wu doe instead of the ContainerControl?

[quote=319099:@Jon Ogden]The code I posted is in a container control that gets embedded in a Window. The handler method for the event is in that Window. That’s in the AddHandler directive.

Should I be adding the handler from the containing Wu doe instead of the ContainerControl?[/quote]
Right, but WHERE in the container? Constructor? An event?

Oh sorry. The code I posted above is in the open event of the container.

Is that why? Since it’s embedded it has to completely open before the parent window does anything?

Interesting…

If I make the Open event of the container as such:

Sub Open() Handles Open
  
  AddHandler me.ContainerInitializedLoadData, AddressOf VWCWin.LoadDataIntoContainer
  AddHandler me.APICommandProcessed, AddressOf VWCWin.CandC_APICommandProcessed
  
  Dim b as Boolean = RaiseEvent ContainerInitializedLoadData(ContainerIndex)
  
  StartingUp = False
End Sub

The Event still fires AFTER the open event completes. I’m wondering if I just can’t Raise and event from inside the open event of the container.

And further contributing to my confusion…

In the handler method for the event, I forced “Return True” so that the boolean variable b above would be true if the event executed. So in the debugger, b is true even though the breakpoint I have in the event code has not happened yet! So that I don’t understand either…

OK. Never mind. My breakpoint in the handler method was not being seen as it was inside an If block that wouldn’t execute if StartingUp is True. So it’s morning and I’ve not had enough coffee. My bad.

I’m embarrassed now…