locked
About delegate RRS feed

  • Question

  • The code is from 70-526 training kit.

    Public Delegate Sub SetProgressDelegate(ByVal val As Integer)
      Public Sub SetProgress(ByVal val As Integer)
        If ProgressBar1.InvokeRequired = True Then
          Dim del As New SetProgressDelegate(AddressOf SetProgress)
          Me.Invoke(del, New Object() {val})
        Else
          ProgressBar1.Value = val
        End If
      End Sub
    I know that delegate is a kind of pointer, my question is SetProgress is referenced within SetProgress. Is it a sort of recursion call?
    Thanks.
    Sunday, October 17, 2010 7:02 PM

Answers

  • Hi Ardmore,

    Even if you are training yourself for an exam, I would post such language specific questions inside a forum about the question. That way your question is read by much more experts than here.

    And yes, it is some kind of recursion. But there are big differences, too. Normaly a recursion happens in one thread only. But in there you have something special regarding the UI. The UI is not thread safe and you should never change the GUI itself in some other thread than the UI thread (Which is just the message loop. So when there is a message to redraw something, it is done and so on.)

    In that example, you have a public member method that is called from another thread. So this thread cannot change the PrograssBar on it's own. So the code is first checking, if a so called invoke is required. If no invoke is required, then the value is set. (In a recursion this is the stop criterium that is required).

    If an invoke is required, a delegate is required and what is happening is something like: A Message is given to the message loop that tells the UI process: Please call this delegate with these arguments. So as soon as this message is processed, the function is called again but this time no invoke is required.

    I hope this helped.

    With kind regards,

    Konrad

    • Marked as answer by ardmore Monday, October 18, 2010 12:49 PM
    Monday, October 18, 2010 8:08 AM
    Answerer