Force an event listener

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eedarley@gmail.com

    #1

    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.

  • Spam Catcher

    #2
    Re: Force an event listener

    eedarley@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.

    Comment

    • eedarley@gmail.com

      #3
      Re: Force an event listener

      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

      Comment

      • Spam Catcher

        #4
        Re: Force an event listener

        eedarley@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


        Comment

        • rowe_newsgroups

          #5
          Re: Force an event listener

          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

          Comment

          • eedarley@gmail.com

            #6
            Re: Force an event listener

            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.

            Comment

            • John

              #7
              Re: Force an event listener

              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

              <eedarley@gmail .comwrote in message
              news:1184334865 .640320.11950@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.
              >

              Comment

              • eedarley@gmail.com

                #8
                Re: Force an event listener

                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

                Comment

                • eedarley@gmail.com

                  #9
                  Re: Force an event listener

                  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.

                  Comment

                  • Phill W.

                    #10
                    Re: Force an event listener

                    eedarley@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.

                    Comment

                    • Patrice

                      #11
                      Re: Force an event listener

                      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

                      <eedarley@gmail .coma écrit dans le message de news:
                      1184336972.5521 29.89600@o61g20 00...legro ups.com...
                      On Jul 13, 10:23 am, Spam Catcher <spamhoney...@r ogers.comwrote:
                      >eedar...@gmail .com wrote in news:1184334865 .640320.11950
                      >@o61g2000hsh.g ooglegroups.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
                      >

                      Comment

                      Working...