473,545 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5596
ee******@gmail. com wrote in news:1184334865 .640320.11950
@o61g2000hsh.go oglegroups.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...@r ogers.comwrote:
eedar...@gmail. com wrote in news:1184334865 .640320.11950
@o61g2000hsh.go oglegroups.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.go oglegroups.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...@r ogers.comwrote:
eedar...@gmail. com wrote in news:1184336972 .552129.89600
@o61g2000hsh.go oglegroups.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(ByVa l value As EventHandler)
_Test = DirectCast([Delegate].Combine(_Test, value), EventHandler)
End AddHandler

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

RaiseEvent(ByVa l sender As Object, ByVal e As System.EventArg s)
If _Test IsNot Nothing Then
_Test.Invoke(se nder, e)
End If
End RaiseEvent
End Event

J

<ee******@gmail .comwrote in message
news:11******** *************@o 61g2000hsh.goog legroups.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 CarAboutToExplo de(ByVal
sender As Car, ByVal e As EventArgs)

Public Sub New(byval evnt as CarAboutToExplo de)
'... 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.CarAboutToE xplode(addresso f OnCarAboutToExp lode)

dim car as new Car(del)
end Sub

Private Sub OnCarAboutToExp lode(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 YouDidntHandleM yEventException ( ...
End If

End Sub

HTH,
Phill W.
Jul 16 '07 #10

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

Similar topics

5
20761
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? I seem to recall NS worked something like this: onmousedown=handleThat;
17
4851
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 event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')'...
2
10090
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 TcpListener(IPAddress.Any, 1234); Stream data = null; Socket socket = null;
6
9147
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 class (seen globally) having a data structure which saves that form data to disk. Whenever the form updates the data I'd like to broadcast the...
0
2065
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 if it doesn't exist yet. The event manager uses weakrefs, so lists of listeners won't stop them
3
1960
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 event to happen. is it possible to accomplish this? thanks!
4
1145
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 self.listeners.keys(): #If the weakref has died, remove it and continue #through the list if listener is None:
2
4933
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 that connection and get the socket. I try to signal an event when this happens and pass that new socket object when the event happens, but when I try...
6
2655
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 enought to do using the preventDefault and stopPropagation event functions. IE's equivalent is supposed to be cancelBubble and returnValue,...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.