Google Groepen ondersteunt geen nieuwe Usenet-berichten of -abonnementen meer. Historische content blijft zichtbaar.

How to check for event listeners?

0 weergaven
Naar het eerste ongelezen bericht

Max

ongelezen,
29 mrt 2004, 06:01:5429-03-2004
aan
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

ongelezen,
29 mrt 2004, 06:52:3129-03-2004
aan
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

ongelezen,
29 mrt 2004, 07:16:2329-03-2004
aan

"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]

ongelezen,
29 mrt 2004, 08:07:0329-03-2004
aan
* "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>

ongelezen,
29 mrt 2004, 08:32:4029-03-2004
aan
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>

ongelezen,
29 mrt 2004, 08:35:2029-03-2004
aan
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]

ongelezen,
29 mrt 2004, 09:43:2629-03-2004
aan
* "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

ongelezen,
30 mrt 2004, 05:40:1230-03-2004
aan

"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

ongelezen,
30 mrt 2004, 05:38:3930-03-2004
aan
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>

ongelezen,
30 mrt 2004, 05:42:4630-03-2004
aan
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 nieuwe berichten