开发者

Infinite Loop and Cross Threading

开发者 https://www.devze.com 2023-03-14 22:37 出处:网络
I am a student attempting to learn VB.NET on my own. Today I wanted to tackle the BackgroundWorker component. I found an excellent article online: How To Use a Background Worker. I successfully comple

I am a student attempting to learn VB.NET on my own. Today I wanted to tackle the BackgroundWorker component. I found an excellent article online: How To Use a Background Worker. I successfully completed the walkthrough and even performed the "adventorous" part that dealt with working with controls updating across threads using delegates.

Now I came to a part that I didn't understand how it was working. To summarize the following code, I have a delegate that has a Label and a String in its signature. I then have a subroutine that is called on the worker thread. Inside this subroutine the delegate is created and (I guess) ran again so that it is on the same (Main) thread. Please correct me if I'm wrong here.

Here is the method is performed on the worker thread:

    Private Sub My_BgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles My_BGWorker.DoWork

        SetLabelText_ThreadSafe(Me.lbl_Status, FormatPercent(i / m_CountTo, 2))

End Sub

I've only included the line of code that is of importance to this question. As you can see this then calls a subroutine that I mentioned:

开发者_开发技巧    Private Sub SetLabelText_ThreadSafe(ByVal lbl As Label, ByVal txt As String)
    ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
    ' If these threads are different, it returns true.
    If lbl.InvokeRequired Then
        'WORKS: Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
        'WORKS: Me.Invoke(MyDelegate, New Object() {lbl, txt})
        MyDel.Invoke(lbl, txt)
    Else
        lbl.Text = txt
    End If
End Sub

Now as you can see I have the code commented out 'WORKS' because it does, but I'm confused as why it goes into an infinite loop when the MyDel.Invoke(lbl, txt) is called because the delegate is created in the Main form Declarations.

Thank you.

EDIT: And for clarification, the delegate in the Main form Declaractions is:

Dim MyDel As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)


When you call this:

Private Sub SetLabelText_ThreadSafe(ByVal lbl As Label, ByVal txt As String)
    If lbl.InvokeRequired Then
        MyDel.Invoke(lbl, txt)

On a background thread, what's happening is this:

  • You run this method on a ThreadPool thread by using BackgroundWorker
  • The method calls lbl.InvokeRequired, which is True, since this is not the UI thread
  • The method then calls Delegate.Invoke, which executes the delegate. This effectively calls the method
  • The method is now running, but still on a non-UI thread, so it immediately sees lbl.InvokeRequired is true, and calls Delegate.Invoke - creating the infinite loop

However, when you call Me.Invoke, this is a bit different. Me, in this context, is the Form, so you're calling Control.Invoke, which marshals the call back to the UI thread. It then runs, but at that point, it'll be running on the UI thread, so lbl.InvokeRequired will be False, and it'll just run once and not go infinite.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号