?
Solved

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

Posted on 2009-02-10
5
Medium Priority
?
495 Views
Last Modified: 2013-11-26
Problem:
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.Items.Add(StringtoAdd)
        Me.ListBox1.SetSelected((ListBox1.Items.Count - 1), True)
        wait_ms(200)
    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.
        BackgroundWorker1.RunWorkerAsync()
    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..."})
        Else
            Me.ListBox1.Items.Add("[0] Begin send process...")
            wait_ms(200)
            Me.ListBox1.Items.Add("[1] Halting Sign ...")
            wait_ms(200)
        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
'TransmitDataOverNetwork
'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.
 
        Else
            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

0
Comment
Question by:KChristenson
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 23615716
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?
0
 

Author Comment

by:KChristenson
ID: 23615995
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.  

Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 23616570
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.
0
 

Author Closing Comment

by:KChristenson
ID: 31545464
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!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 23651862
Glad to be helpful.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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