How do I update a Listbox from a Background Worker after making several function calls?

Posted on 2009-02-10
Last Modified: 2013-11-26
I am using a background worker to handle sending data over a network.  I am using a listbox as a "process-monitor".  I have successfully used invoke to initially update the Listbox as the "Send" process progresses, i.e. BackgroundWorker1.RunWorkerAsync().  There are several occurences during the "Transmit Function" (which lives in a module) where I call the BackgroundWorker1.ReportProgress sub in hopes to again use invoke to update the listbox as I did under (BackgroundWorker1.DoWork)

However, when I check the Listbox1.InvokeRequired property (which was initially true under BackgroundWorker1.DoWork) under the ReportProgress sub the Listbox1.InvokeRequired property is false.  No big deal right?  Just update the Listbox normally.  Well upon investigation, the listbox.items.count method shows 0 items in the listbox at this time...(in which there should be two items); I'm able to add a new item but the contol visible to the user shows only the previous two items I had added using invoke.  What happens to the items in my Listbox?  I want to believe that I'm still on the background worker thread at this time which would require me to use invoke to add more listbox items correct?  How do I get this working correctly?
Delegate Function myMethodDelegate(ByVal StringToReturn As String)

    'Create a delegate for the addListBoxItem Method

    Dim myD1 As New myMethodDelegate(AddressOf Me.addListBoxItem)


  Function addListBoxItem(ByVal StringtoAdd As String)


        Me.ListBox1.SetSelected((ListBox1.Items.Count - 1), True)


    End Function



'***** SEND BUTTON Click ***********************************************************************

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click

' Start the asynchronous operation.


    End Sub



 '***** BACKGROUND WORKER ***********************************************************************

    Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork


' Get the BackgroundWorker object that raised this event.

        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

        BackgroundWorker1.WorkerReportsProgress = True


If Me.ListBox1.InvokeRequired = True Then

            'Invoke the Delegate

            Me.Invoke(myD1, New Object() {"[0] Begin send process..."})

            Me.Invoke(myD1, New Object() {"[0] Halting Sign..."})

            'Me.Invoke(myD1, New Object() {"[0] Testing 111111..."})


            Me.ListBox1.Items.Add("[0] Begin send process...")


            Me.ListBox1.Items.Add("[1] Halting Sign ...")


        End If



        'HALT THE SIGN is our First order of Business

        Call HaltSign(worker, e)


'From the Call HaltSign Function we go to another function called


'no need to go any further with the Background worker at this point

exit sub


'***** TRANSMIT DATA OVER THE NETWORK *****************************

    Public Function TransmitDataOverNetwork(ByVal StringParameter As String, ByVal StringLength As Integer, ByVal worker As BackgroundWorker, _

                                            ByVal e As DoWorkEventArgs)


'Set up IP Address and Port number variables


 Form7.BackgroundWorker1.WorkerReportsProgress = True


'Make a call to Background worker to update Listbox on Form7 (1 of several)

Form7.BackgroundWorker1.ReportProgress(10, ("Establishing Connection to {0}" + TempIPAddress))




end sub



Private Sub backgroundWorker1_ProgressChanged( _

    ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _

    Handles BackgroundWorker1.ProgressChanged


Dim ProgressStatus As Object

        Dim PercentageComplete As Integer


        ProgressStatus = e.UserState

        PercentageComplete = e.ProgressPercentage


        If Me.InvokeRequired = True Then

            'Invoke the Delegate

            Me.Invoke(myD1, New Object() {"[0] " + ProgressStatus})   

' I get this error here if I don't check for Invoke Required (which is false at this point)

'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.



            CheckForIllegalCrossThreadCalls = False

            Call addListBoxItem(ProgressStatus)

'I'm able to add an item to ListBox1 but my previous additions are not there...but are visible on the form...and the Listbox doesn't show this new addition.

        End If

    End Sub

Open in new window

Question by:KChristenson
    LVL 83

    Expert Comment

    When you can use the progress changed event, why are you using the invoke?
    I think the problem lies in this line:
    Form7.BackgroundWorker1.ReportProgress(10, ("Establishing Connection to {0}" + TempIPAddress))

    What is Form7? Is it a new instance of that form or is it a reference to the existing, displayed form?

    Author Comment

    Hey CodeCruiser:

    This is going in the right direction.  Apparently when I return from a function (in a module) because of a call to  to the Form7.BackgroundWorker1.ReportProgress sub I don't need to use invoke...(but I thought I did because I thought I was still on the Background thread).  Form7 is a reference to the existing, displayed form.  I start my background worker on Form7 and then move through several functions and within one of the functions (TransmitDataOverNetwork) I report the progress of my background worker with this -- Form7.BackgroundWorker1.ReportProgress(10, ("Establishing Connection to {0}" + TempIPAddress)).  This call takes me back to Form7 where my backgroundWorker1_ProgressChanged sub is.  This is where I want to again write to the List Box and where I'm having my trouble.  

    LVL 83

    Accepted Solution

    You should not have any trouble. The basic reason for the existance of the backgroundworker is to avoid the invoke procedures. I would suggest to minimize the number of function calls. Try to write most of the code within the dowork procedure.

    Author Closing Comment

    After re-thinking the way I was doing things and also after re-structuring my code to exist within the background worker's Do Work, Progress Changed and Run Worker Completed procedures; there was complete success!  This way I avoided the invoke procedures completely.  Thank you CodeCruiser this was the right call!
    LVL 83

    Expert Comment

    Glad to be helpful.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now