472,975 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 software developers and data experts.

Force an event listener

Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.

Jul 13 '07 #1
10 5559
ee******@gmail.com wrote in news:1184334865.640320.11950
@o61g2000hsh.googlegroups.com:
Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.
AddHandler allows you to assign event handlers to functions.
Jul 13 '07 #2
On Jul 13, 10:23 am, Spam Catcher <spamhoney...@rogers.comwrote:
eedar...@gmail.com wrote in news:1184334865.640320.11950
@o61g2000hsh.googlegroups.com:
Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.

AddHandler allows you to assign event handlers to functions.
Thanks for the response, But what I'm after is not how to handle an
event but ensuring that the consumer is in fact handling my events. So
in my object, before firing off the event, I want to check and see if
in fact the consumer has in fact added an event handler.

Regards

Jul 13 '07 #3
ee******@gmail.com wrote in news:1184336972.552129.89600
@o61g2000hsh.googlegroups.com:
Thanks for the response, But what I'm after is not how to handle an
event but ensuring that the consumer is in fact handling my events. So
in my object, before firing off the event, I want to check and see if
in fact the consumer has in fact added an event handler.
The best way to do this is with a plug-in system.

Create an interface and insure any classes that need to listen to your
events implement the interface.

Your loader class will bind the events to the proper event handlers.

This will ensure:

1. All required handlers are implemented
2. Your loader class attaches all the events to the proper handlers
Jul 13 '07 #4
On Jul 13, 10:58 am, Spam Catcher <spamhoney...@rogers.comwrote:
eedar...@gmail.com wrote in news:1184336972.552129.89600
@o61g2000hsh.googlegroups.com:
Thanks for the response, But what I'm after is not how to handle an
event but ensuring that the consumer is in fact handling my events. So
in my object, before firing off the event, I want to check and see if
in fact the consumer has in fact added an event handler.

The best way to do this is with a plug-in system.

Create an interface and insure any classes that need to listen to your
events implement the interface.

Your loader class will bind the events to the proper event handlers.

This will ensure:

1. All required handlers are implemented
2. Your loader class attaches all the events to the proper handlers
The problem with that is that there isn't a way to force the consumer
to implement the interface (is there?), and also this would mean the
handlers would have to be public.

Would it be possible to pass a delegate in the class's constructor and
map it that way?

Thanks,

Seth Rowe

Jul 13 '07 #5
Seth I think your approach is more friendly. This will require the
consumer to pass itself. From that I can check to see if they are
handling the event. Thanks for everyones help.

Jul 13 '07 #6
Take a look at custom events--you can use them to take control of
the process. In your code for the RaiseEvent method you could
check if there are any subscribers before actually firing the
event. For example:

Private _Test As EventHandler

Public Custom Event Test As EventHandler
AddHandler(ByVal value As EventHandler)
_Test = DirectCast([Delegate].Combine(_Test, value), EventHandler)
End AddHandler

RemoveHandler(ByVal value As EventHandler)
_Test = DirectCast([Delegate].Remove(_Test, value), EventHandler)
End RemoveHandler

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
If _Test IsNot Nothing Then
_Test.Invoke(sender, e)
End If
End RaiseEvent
End Event

J

<ee******@gmail.comwrote in message
news:11*********************@o61g2000hsh.googlegro ups.com...
Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.

Jul 13 '07 #7
Here is an example of what I interpreted to be Seth's approach and
which I am going to use.
Public Class Car
Public Delegate Sub CarAboutToExplode(ByVal
sender As Car, ByVal e As EventArgs)

Public Sub New(byval evnt as CarAboutToExplode)
'... do some stuff
'... oops forgot to put oil in engine
evnt.Invoke(me, eventargs.Empty)
end Sub

Public Sub ShutDown()
'shut down engine
End Sub
end Class

Public Class ConsumerOfCar

Public Sub SomeFunction()
dim del as new Car.CarAboutToExplode(addressof OnCarAboutToExplode)

dim car as new Car(del)
end Sub

Private Sub OnCarAboutToExplode(byval sender as car, bybal e as
eventargs)

sender.ShutDown()

End Sub
End Class

Jul 13 '07 #8
John, I like your approach as well. Thank you for the example. I may
consider that approach as well. It's very clear.

Best Regards.

Jul 13 '07 #9
ee******@gmail.com wrote:
Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.
I'd suggest a custom Event Arguments class to which you add a Handled
property. Pass this along with your Event and, if the Handled property
comes back False ...

Public Class CustomEventArgs
Inherits EventArgs

Friend Sub New()
End Sub

Public Property Handled() as Boolean
Get
Return m_bHandled
End Get
Set( Value as Boolean )
m_bHandled = Value
End Set
End Property

Private m_bHandled = False

End Class

Public Event SomeEvent( _
ByVal sender As Object _
, ByVal e As CustomEventArgs _
)

Protected Sub OnSomeEvent()
Dim e as New CustomEventArgs

RaiseEvent SomeEvent( Me, e )

If e.Handled = False Then
Throw New YouDidntHandleMyEventException( ...
End If

End Sub

HTH,
Phill W.
Jul 16 '07 #10
This is not needed as this is done for you ? (not sure what is the exact
problem).

If you meant you want to know if a particuler event has been handled, in
most cases this is done using a "Handled" boolean in the event arguments.
See for example :
http://msdn2.microsoft.com/en-us/lib...s.handled.aspx

You can then take the appropriate default action if this boolean is still
false (the consumer could also have registered something but not being
insterested in this particular event).

--
Patrice

<ee******@gmail.coma écrit dans le message de news:
11*********************@o61g2000hsh.googlegroups.c om...
On Jul 13, 10:23 am, Spam Catcher <spamhoney...@rogers.comwrote:
>eedar...@gmail.com wrote in news:1184334865.640320.11950
@o61g2000hsh.googlegroups.com:
Is there a way to force the consumer of my object to handle events
that I've fired off? I don't care how they handle the event, just that
they do. I think in c# there is a way but not sure about vb.net.
Please advise.

AddHandler allows you to assign event handlers to functions.
Thanks for the response, But what I'm after is not how to handle an
event but ensuring that the consumer is in fact handling my events. So
in my object, before firing off the event, I want to check and see if
in fact the consumer has in fact added an event handler.

Regards

Jul 16 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
2
by: Craig | last post by:
Hi I listen on a port, when data is received I raise an event (OnMessageReceived) in the while loop as follows: private void WaitForConnection() { TcpListener listener = new...
6
by: Steve Teeples | last post by:
I have been perplexed by how to best treat an event that spans different classes. For example, I have a form which a user inputs data. I want to broadcast that data via an event to another...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
3
by: sowencheung | last post by:
I attach an Event in document.onkeyup and another event in document.onbeforeunload in onkeyup event, if the condition is satisfied, i will activate sth, then i don't want the onbeforeunload...
4
by: steve | last post by:
The example code from: http://sjbrown.ezide.com/games/example1.py.html .... def Notify( self, event ): if not isinstance(event, TickEvent): Debug( " Message: " + event.name ) for listener in...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
6
by: blaine | last post by:
Hello, I'm currently overriding function keys (F1 to F4) to perform other actions. In order to do this the default popup windows of Help (F1), Seach(F3) etc must be turned off. In FF it's easy...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.