How to check for event listeners?

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I know that in VB.Net I don't need to check if an events has some listeners,
because raiseevent do that for me...
but I WANT know if the event has some listeners, because IF it has someone,
I do some cpu intensive works.


Public Event CarRead(ByVal sender As Object, ByVal MyData As
MyLittleInformation)

Protected Overridable Sub OnCarRead(ByVal MyData As MyLittleInformation)

Dim MyDataInternal as MyBigData

MyDataInternal =
MyBigProcedureThatRequireManyReoursesToPrepareMyData(MyLittleInformation)

RaiseEvent CarRead(Me, MyDataInternal)

MyBigProcedureThatRequireManyReoursesToAnalyzeMyData(MyDataInternal)

End Sub



The 2 procedure MyBigProcedureThatRequireManyReoursesToPrepareMyData /
MyBigProcedureThatRequireManyReoursesToAnalyzeMyData
require time and resources, so it's a bad thing to execute them if there are
no event listeners.

But I don't know how to check if there are event listener in VB.Net.

Hwo can I handle this in VB.Net?

Thanks, Max
 
Hi Max,

It is not necessary that I understand it, but what do you mean with this.
I know that in VB.Net I don't need to check if an events has some listeners,
because raiseevent do that for me...
but I WANT know if the event has some listeners, because IF it has someone,
I do some cpu intensive works.

Do you know how many events it take when you put some data on the screen or
when you move with your mouse upon it, which you cannot disable?

What kind of events you want to prevent?

Cor
 
Cor said:
Hi Max,

It is not necessary that I understand it, but what do you mean with this.


Do you know how many events it take when you put some data on the screen or
when you move with your mouse upon it, which you cannot disable?

I don't want to disable .Net built-in events.
I'm working in self-developed class that has self-developer events.
I want to prevent the execution of the event-management code if ther are no
event listeners.
I know how to do that in c#; but I didn't find how to do that in VB.Net.
What kind of events you want to prevent?

I'm not tring to prevent event, I wont to be able to knoe if there are event
listeners.

In my class, I have some events that inquiry a DB in the event managemebt
routine (OnXXXXX)
Clearly, it is bad to inquiry the DB if ther are non listener to the events.

Thanks, Max
 
* "Max said:
I know that in VB.Net I don't need to check if an events has some listeners,
because raiseevent do that for me...
but I WANT know if the event has some listeners, because IF it has someone,
I do some cpu intensive works.

This sample will show you how to get the number of handlers registered
for an event:

\\\
Public Class Main
Public Shared Sub Main()
Dim c As New FooBar()
AddHandler c.Foo, AddressOf Goo
c.AddSampleHandler()
c.AddSampleHandler()
Console.WriteLine( _
"Anzahl der Handler für Foo: {0}", _
c.NumberOfFooHandlers _
)
RemoveHandler c.Foo, AddressOf Goo
Console.Read()
End Sub

Private Shared Sub Goo()
End Sub
End Class

Public Class FooBar
Public Event Foo()

Public ReadOnly Property NumberOfFooHandlers() As Integer
Get
Return FooEvent.GetInvocationList().Length
End Get
End Property

Public Sub AddSampleHandler()
AddHandler Foo, AddressOf Moo
End Sub

Private Sub Moo()
End Sub
End Class
///
 
hi Herfried,

the problem with your example is that it will crash if no event handlers have
been added, because the FooEvent multicast delegate will be a null reference.
For Max's needs a simpel If FooEvent Is Nothing will do

Bill
 
Hi Max,

VB creates a multicast delegate with the name of the event & "Event". So for
your CarRead event, their is actually a multicast delegate field who's name is
CarReadEvent. To see if that has any listeners, check to see if it is nothing.
e.g :

Public Event CarRead(ByVal sender As Object, ByVal MyData As
MyLittleInformation)

Protected Overridable Sub OnCarRead(ByVal MyData As MyLittleInformation)

Dim MyDataInternal as MyBigData

If Not CarReadEvent Is Nothing Then
MyDataInternal =
MyBigProcedureThatRequireManyReoursesToPrepareMyData(MyLittleInformation)

End If
RaiseEvent CarRead(Me, MyDataInternal)

MyBigProcedureThatRequireManyReoursesToAnalyzeMyData(MyDataInternal)

End Sub


Bill.
 
* "Bill McCarthy said:
the problem with your example is that it will crash if no event handlers have
been added, because the FooEvent multicast delegate will be a null reference.
For Max's needs a simpel If FooEvent Is Nothing will do

Ooops. Thank you for making me aware of that!
 
Bill McCarthy said:
Hi Max,

VB creates a multicast delegate with the name of the event & "Event". So for
your CarRead event, their is actually a multicast delegate field who's name is
CarReadEvent. To see if that has any listeners, check to see if it is nothing.
e.g :


Thanks you very much!!!!!!!!!!!!!!!!!!!!!!!

but... where did you find this information?

Max
 
Hi Max,

uhm, from working closely with Vb.NET for so many years (oh and ILDASM helps a
lot too ;) )
 
Back
Top