Rasied Events traverse the call stack until handled

B

Bob Day

Using vs 2003, vb.net

It is not clear from the documentation, but it appears that a raised event
will traverse up the call stack upwards until it reaches its handler. Is
this correct? See the code snippet below. The event raised in sub1 or
class three will be caught in Class1 event handler (I know below is not
syntactically correct, but you get the idea).


Class 1 '------------------
friend Class2 as Class2x
Sub TestEvent_EventHandler Handles TestEvent
' raised event in class 3 caught here
end sub

' call something in class 2

end class

Class2 '------------------

friend Class3 as class 3x

Call Sub1 in class3

end class

Class 3 '------------------

Event TestEvent

Sub 1

RaiseEvent TestEvent

end sub

End class

please advise.

Bob Day
 
G

Gary Chang

Hi Bob,

Thanks for using Microsoft MSDN Managed Newsgroup. My name is Gary, and I
will be assisting you on this issue.

Currently I am looking for somebody who could help you on it. We will reply
here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
M

Mattias Sjögren

It is not clear from the documentation, but it appears that a raised event
will traverse up the call stack upwards until it reaches its handler. Is
this correct?

No, when you raise an event you just en up calling the list of handler
methods sequentially. There's no stack walking going on.



Mattias
 
B

Bob Day

Whats the practical difference? If the handler is 3 levels up from where
the event was raise, it will be caught 3 levels up from where the event was
raised. Is this not the same as traversing the call stack up?

On a related mater, if multiple threads call the instantiated class that may
raise the same event, and all threads have the same handlers 3 levels up,
only the handler that will be trigger is the one that instantiated the
class, correct? Certainly handlers in other classes or threads would not be
triggered. I think the answer is yes - it would follow the stack back up to
its handler, even if there were identical handlers in other classes..

Your thoughts.

Thanks!

Bob Day
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
Are you confusing RaiseEvent with Throw exception?

As Mattias stated RaiseEvent does NOT tranverse stack frames, it calls each
handler added to the Event with AddHandler or Handles in turn, then returns
you to the current stack frame.

Throw exception will traverse up the call stack until a Catch block is
found.

From you description it sounds like you are talking Throw exception, while
your sample is (attempting) to show RaiseEvent.

Hope this helps
Jay
 
P

Peter Huang

Hi Bob,

I agree with Mattias and Jay's suggestion.
Also, when you call an method which raises the event in multiple threads,
all the handler functions added to the handler will be called.
Here I write a demo for you, you may take a look.
Imports System.Threading
Module Module1
Public Class TestEvent
Public Event TestHelloEvent()
Public Sub Test()
RaiseEvent TestHelloEvent()
End Sub
End Class
Dim o As TestEvent
Public Sub ThreadProc()
AddHandler o.TestHelloEvent, AddressOf hello
Console.WriteLine("ThreadProc: ")
o.Test()
'The function will call both the o_TestHelloEvent and hello
function. so far there are two event handler function on this instantiated
class o
End Sub

Sub Main()
Thread.CurrentThread.Name = "Main Thread"
o = New TestEvent
AddHandler o.TestHelloEvent, AddressOf o_TestHelloEvent
o.Test()
'The one will only call the o_TestHelloEvent,so far there is only
on event handler funtion,
'on this instantiated class o

Dim th As New Thread(AddressOf ThreadProc)
th.Name = "Forked Thread"
th.Start()
th.Join(3000)
'Wait for the Forked Thread exit
o.Test()
'the one will call both the o_TestHelloEvent and hello function.
End Sub

Public Sub hello()
Console.WriteLine("Event Fired hello From " +
Thread.CurrentThread.Name)
End Sub
Private Sub o_TestHelloEvent()
Console.WriteLine("Event Fired o_TestHelloEvent From " +
Thread.CurrentThread.Name)
End Sub
End Module

In addition, event is a kind of delegate which can be understood it as a
function point.
Here are two links you may take a look.
Raising an Event
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconprovidingeventfunctionality.asp
Events and Delegates
http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconeventsdelegates.aspx

If you have more concern on this issue, please post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top