Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to check for event listeners?

0 views
Skip to first unread message

Max

unread,
Mar 29, 2004, 6:01:54 AM3/29/04
to
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

Cor

unread,
Mar 29, 2004, 6:52:31 AM3/29/04
to
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


Max

unread,
Mar 29, 2004, 7:16:23 AM3/29/04
to

"Cor" <n...@non.com> ha scritto nel messaggio
news:eriTFTY...@tk2msftngp13.phx.gbl...

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


Herfried K. Wagner [MVP]

unread,
Mar 29, 2004, 8:07:03 AM3/29/04
to
* "Max" <megam...@yahoo.it> scripsit:

> 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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Bill McCarthy <no spam>

unread,
Mar 29, 2004, 8:32:40 AM3/29/04
to
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


"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:c4971n$2e1d86$1...@ID-208219.news.uni-berlin.de...

Bill McCarthy <no spam>

unread,
Mar 29, 2004, 8:35:20 AM3/29/04
to
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.

"Max" <megam...@yahoo.it> wrote in message
news:uTW5A1XF...@TK2MSFTNGP11.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Mar 29, 2004, 9:43:26 AM3/29/04
to
* "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> scripsit:

> 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!

Max

unread,
Mar 30, 2004, 5:40:12 AM3/30/04
to

"Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> ha scritto nel
messaggio news:OQljgKZF...@TK2MSFTNGP11.phx.gbl...

> 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

Max

unread,
Mar 30, 2004, 5:38:39 AM3/30/04
to
Thanks you very much!!!!!!!!!!!!!!!!!!!!!!!

This is exactly wath I need!

Max


"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> ha scritto nel
messaggio news:c4971n$2e1d86$1...@ID-208219.news.uni-berlin.de...

Bill McCarthy <no spam>

unread,
Mar 30, 2004, 5:42:46 AM3/30/04
to
Hi Max,

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

"Max" <megam...@yahoo.com> wrote in message
news:edOkjNkF...@tk2msftngp13.phx.gbl...

0 new messages