event vs. delegate vs. threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ondrej Sevecek

    #1

    event vs. delegate vs. threads

    Hello,
    what is the difference between "event" and the only use of delegate? Why
    one should note events with "event" keyword when the functionality seems the
    same as with pure delegates?

    When I call some delegate, in which thread this delegate gets called -
    callers = synchronous, or some other thread = asynchronous? And in what
    thread is this done with "event"?

    Ondra.



  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: event vs. delegate vs. threads

    Hello Ondrej,
    [color=blue]
    > what is the difference between "event" and the only use of delegate? Why
    > one should note events with "event" keyword when the functionality seems[/color]
    the[color=blue]
    > same as with pure delegates?[/color]

    This improves code readability. A notion of event is much more
    understandable.
    Then, it is more convenient to raise events than to invoke delegates.
    [color=blue]
    > When I call some delegate, in which thread this delegate gets called -
    > callers = synchronous, or some other thread = asynchronous? And in what
    > thread is this done with "event"?[/color]

    In both cases, calls are synchronous, unless you use BeginInvoke.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in message
    news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hello,
    > what is the difference between "event" and the only use of delegate? Why
    > one should note events with "event" keyword when the functionality seems[/color]
    the[color=blue]
    > same as with pure delegates?
    >
    > When I call some delegate, in which thread this delegate gets called -
    > callers = synchronous, or some other thread = asynchronous? And in what
    > thread is this done with "event"?
    >
    > Ondra.
    >
    >
    >[/color]

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: event vs. delegate vs. threads

      Ondra,

      With events, the firing of the event is restricted to the class that
      defined the event. This is a good thing, because if you just exposed a
      public field that was a delegate, then anyone could invoke the delegate, and
      you would have undesirable behavior.

      When you call an event or a delegate synchronously, the event handlers
      execute on whatever thread the delegate was invoked on. When you call it
      asynchronously, the call will be executed on a thread other than the thread
      that initiated the call.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in message
      news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > Hello,
      > what is the difference between "event" and the only use of delegate? Why
      > one should note events with "event" keyword when the functionality seems[/color]
      the[color=blue]
      > same as with pure delegates?
      >
      > When I call some delegate, in which thread this delegate gets called -
      > callers = synchronous, or some other thread = asynchronous? And in what
      > thread is this done with "event"?
      >
      > Ondra.
      >
      >
      >[/color]


      Comment

      • Cowboy \(Gregory A. Beamer\)

        #4
        Re: event vs. delegate vs. threads

        "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in message
        news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Hello,
        > what is the difference between "event" and the only use of delegate? Why
        > one should note events with "event" keyword when the functionality seems[/color]
        the[color=blue]
        > same as with pure delegates?[/color]

        Behind the scenes, the functionality is pretty much the same. The delegate
        is a bit more flexible, as you can hook multiple handlers in to create
        multi-casting "events".
        [color=blue]
        > When I call some delegate, in which thread this delegate gets called -
        > callers = synchronous, or some other thread = asynchronous? And in what
        > thread is this done with "event"?[/color]

        Unless you code it otherwise, the default is synch.

        --
        Gregory A. Beamer
        MVP; MCP: +I, SE, SD, DBA

        *************** *************** *************** *************** **********
        Think Outside the Box!
        *************** *************** *************** *************** **********


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: event vs. delegate vs. threads

          Ondrej,
          In addition to the others comments:

          The way I view it is:

          Events are implemented in terms of a Delegate, similar to how a Property is
          implemented in terms of a Field.

          The Event protects the underlying Delegate, similar to how a Property
          protects the underlying Field.

          In other words you use Events when you want to encapsulate the Delegate,
          similar to how you use a Property when you want to encapsulate a Field.

          Hope this helps
          Jay

          "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in message
          news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > Hello,
          > what is the difference between "event" and the only use of delegate? Why
          > one should note events with "event" keyword when the functionality seems[/color]
          the[color=blue]
          > same as with pure delegates?
          >
          > When I call some delegate, in which thread this delegate gets called -
          > callers = synchronous, or some other thread = asynchronous? And in what
          > thread is this done with "event"?
          >
          > Ondra.
          >
          >
          >[/color]


          Comment

          • Alvin Bruney

            #6
            Re: event vs. delegate vs. threads

            Properties are methods, not fields.


            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:uMUpaNcuDH A.1996@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Ondrej,
            > In addition to the others comments:
            >
            > The way I view it is:
            >
            > Events are implemented in terms of a Delegate, similar to how a Property[/color]
            is[color=blue]
            > implemented in terms of a Field.
            >
            > The Event protects the underlying Delegate, similar to how a Property
            > protects the underlying Field.
            >
            > In other words you use Events when you want to encapsulate the Delegate,
            > similar to how you use a Property when you want to encapsulate a Field.
            >
            > Hope this helps
            > Jay
            >
            > "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in[/color]
            message[color=blue]
            > news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=green]
            > > Hello,
            > > what is the difference between "event" and the only use of delegate?[/color][/color]
            Why[color=blue][color=green]
            > > one should note events with "event" keyword when the functionality seems[/color]
            > the[color=green]
            > > same as with pure delegates?
            > >
            > > When I call some delegate, in which thread this delegate gets called -
            > > callers = synchronous, or some other thread = asynchronous? And in what
            > > thread is this done with "event"?
            > >
            > > Ondra.
            > >
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: event vs. delegate vs. threads

              Alvin,
              Did you read what I stated? Did you understand what I said?

              No place did I state a Property IS A Field, I made an analogy relating a
              property to a field, however that is relating not stating is a!

              I know properties are methods. It happens to be a method that normally
              encapsulates a field.

              Hope this helps
              Jay

              "Alvin Bruney" <alvin.bruney@. telia..com.> wrote in message
              news:%23twl8Wcu DHA.2464@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > Properties are methods, not fields.
              >
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              > news:uMUpaNcuDH A.1996@TK2MSFTN GP12.phx.gbl...[color=green]
              > > Ondrej,
              > > In addition to the others comments:
              > >
              > > The way I view it is:
              > >
              > > Events are implemented in terms of a Delegate, similar to how a Property[/color]
              > is[color=green]
              > > implemented in terms of a Field.
              > >
              > > The Event protects the underlying Delegate, similar to how a Property
              > > protects the underlying Field.
              > >
              > > In other words you use Events when you want to encapsulate the Delegate,
              > > similar to how you use a Property when you want to encapsulate a Field.
              > >
              > > Hope this helps
              > > Jay
              > >
              > > "Ondrej Sevecek" <ondra@sevecek. com.thisisanunt ispamsuffix> wrote in[/color]
              > message[color=green]
              > > news:euRix%23Zu DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
              > > > Hello,
              > > > what is the difference between "event" and the only use of delegate?[/color][/color]
              > Why[color=green][color=darkred]
              > > > one should note events with "event" keyword when the functionality[/color][/color][/color]
              seems[color=blue][color=green]
              > > the[color=darkred]
              > > > same as with pure delegates?
              > > >
              > > > When I call some delegate, in which thread this delegate gets[/color][/color][/color]
              called -[color=blue][color=green][color=darkred]
              > > > callers = synchronous, or some other thread = asynchronous? And in[/color][/color][/color]
              what[color=blue][color=green][color=darkred]
              > > > thread is this done with "event"?
              > > >
              > > > Ondra.
              > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...