Delegates help

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

    Delegates help

    I am trying to understand delegates and I think I do understand them ...
    just hoping if someone can tell me im on the right track.
    So far what I have read is that a Delegate is an Asynchronus call to a
    method or function or whatever...

    So basically you create an object that references the address of the
    meathod.

    By calling this delegate - you get a 'return' right away so your code can
    continue and the delegate runs in a different thread doing whatever the
    function / meathod is required.

    Is this the correct assumption?
    The example in the book is pretty simple ( its a writeline command ).

    So as far as 'uses' for delegates ( if im on the right track )...its useful
    when you have a lot to process, but do not want to do it in the main thread
    ( or the app will temporarily stop working ), you use a delegate to call the
    meathod in a different thread.

    Is this correct?

    Thanks,

    Miro

  • Adam Benson

    #2
    Re: Delegates help

    Is this correct?
    Not at all

    I don't know if you're familiar with C or C++ (ignore this sentence if you
    aren't) but if you are then the closest to them that you're familiar with
    are function pointers.

    In C# I guess you could think of them as a variable for method calls. Like
    variables they have a type, and can be instantiated. An instance of a
    delegate type can be called (invoked) and that will result in the method the
    delegate instance points at being called.

    So a delegate has a signature or type describing the kind of method it can
    point to. A piece of code may say "If you want to know when I've done this
    then I'll tell you - give me a delegate of such and such a type and I'll
    call it when I've done it."

    The bit about delegates executing on another thread - well, I don't know
    where that came from, but a delegate call will execute on the same thread
    that called it.

    This is a bit of a loose definition, but I hope it gets you started.

    Best way to figure this out is just to play with it. Create a form with a
    button on and aim to pop up a message box when the button is clicked on.
    That involves the use of delegates. Or type "delegate" in Visual Studio and
    hit F1. That will call up the help for it.

    Cheers,

    Adam.


    "Miro" <miro@beero.com wrote in message
    news:OqTPHYm7IH A.3976@TK2MSFTN GP06.phx.gbl...
    >I am trying to understand delegates and I think I do understand them ...
    >just hoping if someone can tell me im on the right track.
    So far what I have read is that a Delegate is an Asynchronus call to a
    method or function or whatever...
    >
    So basically you create an object that references the address of the
    meathod.
    >
    By calling this delegate - you get a 'return' right away so your code can
    continue and the delegate runs in a different thread doing whatever the
    function / meathod is required.
    >
    Is this the correct assumption?
    The example in the book is pretty simple ( its a writeline command ).
    >
    So as far as 'uses' for delegates ( if im on the right track )...its
    useful when you have a lot to process, but do not want to do it in the
    main thread ( or the app will temporarily stop working ), you use a
    delegate to call the meathod in a different thread.
    >
    Is this correct?
    >
    Thanks,
    >
    Miro

    Comment

    • Miro

      #3
      Re: Delegates help

      I might still be missing something but I see how the delegate points to the
      function/method :

      But I still seem to be missing why I wouldnt call the sub "DisplayMessage "
      directly? Why delegate it?

      Thanx,

      Miro

      =====
      Public Class Form1

      Delegate Sub MyFirstDelegati on(ByVal blaTxt As String)


      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      Dim deleg As MyFirstDelegati on
      deleg = New MyFirstDelegati on(AddressOf DisplayMessage)

      deleg.Invoke("W hy not just call display message directly")

      Debug.WriteLine ("when did this line get hit?")
      End Sub

      Sub DisplayMessage( ByVal msgText As String)
      MessageBox.Show (msgText, "delegation ")
      End Sub

      End Class
      =====

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Delegates help

        Miro <miro@beero.com wrote:
        I might still be missing something but I see how the delegate points to the
        function/method :
        >
        But I still seem to be missing why I wouldnt call the sub "DisplayMessage "
        directly? Why delegate it?
        In this case, you wouldn't. The point is to be able to effectively pass
        some logic to other pieces of code.

        See http://csharpindepth.com/Articles/Ch.../Closures.aspx for an
        article which concentrates on closures, but should also give you a
        flavour of why delegates are a good thing.

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • breitak67

          #5
          Re: Delegates help


          The part about executing on another thread is probably an incomplete
          reference to the fact that when an event is raised from a thread other
          than the main GUI thread the event handler executes in the context of
          that thread. If the event handler has to update controls owned by the
          GUI thread you need to use a delegate and Invoke method to punt the
          execution of the handler code that updates the controls to the GUI
          thread (otherwise you will likely get a cross-threading exception).
          Summary: delegates can, in combination with the Invoke method, pass
          execution of the delegate method's code to the main GUI thread from
          another thread.


          --
          breitak67

          Comment

          Working...