KChristenson
asked on
How do I update a Listbox from a Background Worker after making several function calls?
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.RunWorke rAsync(). There are several occurences during the "Transmit Function" (which lives in a module) where I call the BackgroundWorker1.ReportPr ogress 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?
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.RunWorke
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
ASKER
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.Re portProgre ss 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.Re portProgre ss(10, ("Establishing Connection to {0}" + TempIPAddress)). This call takes me back to Form7 where my backgroundWorker1_Progress Changed sub is. This is where I want to again write to the List Box and where I'm having my trouble.
Thanks
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.Re
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Glad to be helpful.
I think the problem lies in this line:
Form7.BackgroundWorker1.Re
What is Form7? Is it a new instance of that form or is it a reference to the existing, displayed form?