Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:59:33+00:00 2026-05-23T08:59:33+00:00

I am a student attempting to learn VB.NET on my own. Today I wanted

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:59:34+00:00Added an answer on May 23, 2026 at 8:59 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Background I am a student just beginning to learn MIPS for one of
I am attempting to help a student write an android app that will contact
TABLES: student(name,points) playsin(name->student(name), score) I'm trying to use: UPDATE STUDENT SET points = points
I am attempting to create my own hash class for a school assignment, but
I am a student and I always develop projects on my own, mainly with
I am attempting to run a number of student FORTRAN programs from a python
Here's the problem code I'm attempting to compile: bool TeamMatcher::simpleComparator(Student first, Student second){ return
A student in my class brought this to my attention, and I didn't have
INSERT INTO [Temp].[dbo].[Student] ([Fname], [Lname], [Gender]) VALUES (N'Aname', N'Alname', N'Male') GO This codes workes
I have Class and Student objects. Both have collection of another as property. Which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.