newyuppie
asked on
BackgroundWorker RunWorkerCompleted never fires!
vb2005
hi,
Im using a backworker called from a form's load event. this backworker runs some code (which does not modify the UI at all, and does not have any exception handling). the code effectively finishes running (i have debugged through every line), but the RunWorkerCompleted event, in which i do some cleanup and modification of UI) never gets fired at all.
I had Application.DoEvents() in a loop while I waited in the Load event for the worker to finish, and it used to work fine, apparently firing the RunWorkerCompleted event, because I had no issues. But since I removed the DoEvents from the Load, RunWorkerCompleted doesnt fire. The reason I removed DoEvents is because it started to raise exceptions, though it hadn't done before at all.
Anybody got any ideas?
NY
hi,
Im using a backworker called from a form's load event. this backworker runs some code (which does not modify the UI at all, and does not have any exception handling). the code effectively finishes running (i have debugged through every line), but the RunWorkerCompleted event, in which i do some cleanup and modification of UI) never gets fired at all.
I had Application.DoEvents() in a loop while I waited in the Load event for the worker to finish, and it used to work fine, apparently firing the RunWorkerCompleted event, because I had no issues. But since I removed the DoEvents from the Load, RunWorkerCompleted doesnt fire. The reason I removed DoEvents is because it started to raise exceptions, though it hadn't done before at all.
Anybody got any ideas?
NY
Can you post the code for the BacgroundWorker?
ASKER
ok here it goes
Private Sub BackWorkerLayoutCombos_DoW ork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWo rkEventArg s) Handles BackWorkerLayoutCombos.DoW ork
Console.WriteLine("Layout Combos Begin")
GenericList.Clear()
GenericList.Add(New ListItem("Nacionalidad", NacionalidadComboBox))
GenericList.Add(New ListItem("Edad", EdadComboBox))
GenericList.Add(New ListItem("Genero", GeneroComboBox))
GenericList.Add(New ListItem("Estado Civil", Estado_CivilComboBox, "Estado_Civil"))
... ' more of the same
If MySqlConnection.State = ConnectionState.Closed Then
'this actually never enters because connection is open all the time
MySqlConnection.Open()
End If
For Each li As ListItem In GenericList
Dim dt As New DataTable("ResultCategoria ")
MySqlCommandStorProc.Param eters("PCa tegoria"). Value = li.Text
MySqlCommandStorProc.Execu teNonQuery ()
Me.ProporcionesMySqlDataAd apter.Fill (dt)
DataTableList.Add(dt)
Next li
End Sub
Private Sub BackWorkerLayoutCombos_Run WorkerComp leted(ByVa l sender As Object, ByVal e As System.ComponentModel.RunW orkerCompl etedEventA rgs) Handles BackWorkerLayoutCombos.Run WorkerComp leted
For Each dt As DataTable In DataTableList
Dim StoredLi As ListItem = CType(GenericList.Item(Dat aTableList .IndexOf(d t)), ListItem)
Dim ctl As ComboBox = DirectCast(StoredLi.Contro l, ComboBox)
ctl.BeginUpdate()
ctl.DataSource = dt
ctl.Tag = dt.Rows(0).Item("Tipo_Vari able").ToS tring
ctl.EndUpdate()
Dim DefaultIndex As Integer = -1
Dim CategoriaField As String = StoredLi.Text
Dim CategoriaFieldMapping As String = StoredLi.SolicitantesColum nMapping
Dim DefaultSubCategoria As String = Nothing
For i As Integer = 0 To (dt.Rows.Count - 1)
If (dt.Rows(i).Item("IsDefaul t") IsNot System.DBNull.Value) Then
If dt.Rows(i).Item("IsDefault ") = 1 Then
DefaultIndex = i
DefaultSubCategoria = dt.Rows(i).Item("Subcatego ria")
Exit For
End If
End If
Next i
If MySQLDataset.solicitantes. Columns.Co ntains(Cat egoriaFiel dMapping) Then
MySQLDataset.solicitantes. Columns(Ca tegoriaFie ldMapping) .DefaultVa lue = DefaultSubCategoria
End If
Next
_BackLayoutCombosDone = True
Console.WriteLine("Layout Combos End")
End Sub
Private Sub BackWorkerLayoutCombos_DoW
Console.WriteLine("Layout Combos Begin")
GenericList.Clear()
GenericList.Add(New ListItem("Nacionalidad", NacionalidadComboBox))
GenericList.Add(New ListItem("Edad", EdadComboBox))
GenericList.Add(New ListItem("Genero", GeneroComboBox))
GenericList.Add(New ListItem("Estado Civil", Estado_CivilComboBox, "Estado_Civil"))
... ' more of the same
If MySqlConnection.State = ConnectionState.Closed Then
'this actually never enters because connection is open all the time
MySqlConnection.Open()
End If
For Each li As ListItem In GenericList
Dim dt As New DataTable("ResultCategoria
MySqlCommandStorProc.Param
MySqlCommandStorProc.Execu
Me.ProporcionesMySqlDataAd
DataTableList.Add(dt)
Next li
End Sub
Private Sub BackWorkerLayoutCombos_Run
For Each dt As DataTable In DataTableList
Dim StoredLi As ListItem = CType(GenericList.Item(Dat
Dim ctl As ComboBox = DirectCast(StoredLi.Contro
ctl.BeginUpdate()
ctl.DataSource = dt
ctl.Tag = dt.Rows(0).Item("Tipo_Vari
ctl.EndUpdate()
Dim DefaultIndex As Integer = -1
Dim CategoriaField As String = StoredLi.Text
Dim CategoriaFieldMapping As String = StoredLi.SolicitantesColum
Dim DefaultSubCategoria As String = Nothing
For i As Integer = 0 To (dt.Rows.Count - 1)
If (dt.Rows(i).Item("IsDefaul
If dt.Rows(i).Item("IsDefault
DefaultIndex = i
DefaultSubCategoria = dt.Rows(i).Item("Subcatego
Exit For
End If
End If
Next i
If MySQLDataset.solicitantes.
MySQLDataset.solicitantes.
End If
Next
_BackLayoutCombosDone = True
Console.WriteLine("Layout Combos End")
End Sub
By the looks of the code it should be working. Try placing
Console.WriteLine("DoWork Done")
at the end of BackWorkerLayoutCombos_DoW ork and
Console.WriteLine("RunWork erComplete d Entered")
At the beginning of BackWorkerLayoutCombos_Run WorkerComp leted to see if it gets to those points.
Console.WriteLine("DoWork Done")
at the end of BackWorkerLayoutCombos_DoW
Console.WriteLine("RunWork
At the beginning of BackWorkerLayoutCombos_Run
ASKER
it gets to the DoWork done, but never enters the RunWorkerCompleted...
ASKER
i call the AsyncRun in the Load event of the form, then further down on the load event i placed a loop to wait for it to finish before doing anything else.
Do
If _BackLayoutCombosDone Then
Exit Do
End If
'Application.DoEvents()
Loop While True
I used to have the DoEvents inside, and it used to work out like that. But now DoEvents does not work, so i commented it out...
Do
If _BackLayoutCombosDone Then
Exit Do
End If
'Application.DoEvents()
Loop While True
I used to have the DoEvents inside, and it used to work out like that. But now DoEvents does not work, so i commented it out...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sounds reasonable, the thing is, there are about 90 controls in the form loading... is there a way to "flush" the form loop, or to bump the event up in the loop?
another thing, why do you think App.DoEvents() is Exceptioning?
thanks for all your help fernando
another thing, why do you think App.DoEvents() is Exceptioning?
thanks for all your help fernando
Hi newyuppie;
To your question, "the thing is, there are about 90 controls in the form loading", You can do it all at one time by executing the statement Me.Enabled = False and at the end of the RunWorkerCompleted do a Me.Enabled = True.
To your question, "is there a way to "flush" the form loop, or to bump the event up in the loop?", The Application.DoEvents() is about the only thing I know that will service the messages from the queue.
To your question, "why do you think App.DoEvents() is Exceptioning?", What is the exception that it is throwing?
Fernando
To your question, "the thing is, there are about 90 controls in the form loading", You can do it all at one time by executing the statement Me.Enabled = False and at the end of the RunWorkerCompleted do a Me.Enabled = True.
To your question, "is there a way to "flush" the form loop, or to bump the event up in the loop?", The Application.DoEvents() is about the only thing I know that will service the messages from the queue.
To your question, "why do you think App.DoEvents() is Exceptioning?", What is the exception that it is throwing?
Fernando
ASKER
hey fernando,
ok, so i've modified a few things here and there, and it finally is working, and DoEvents no longer throws an exception.
The thing was the following: as you pointed out, there actually were some things that were bottling up in the form's message loop. I inadvertently left in the Load event of the form another call to a different BackWorker, which did indeed modify some controls in the UI. So, as per your comment, I traced down the error to some events that DoEvents was bringing to the front before their time, so that it produced a NullReference exception (control hadnt been completely created and i was already tying events and modifications to it through the second BackWorker).
I took what you said in this and some other questions i had today, and fixed that BackWorker to handle UI things outside of the DoWork procedures, and it all appears to be working fine right now.
Thanks for your time and wisdom
NY
ok, so i've modified a few things here and there, and it finally is working, and DoEvents no longer throws an exception.
The thing was the following: as you pointed out, there actually were some things that were bottling up in the form's message loop. I inadvertently left in the Load event of the form another call to a different BackWorker, which did indeed modify some controls in the UI. So, as per your comment, I traced down the error to some events that DoEvents was bringing to the front before their time, so that it produced a NullReference exception (control hadnt been completely created and i was already tying events and modifications to it through the second BackWorker).
I took what you said in this and some other questions i had today, and fixed that BackWorker to handle UI things outside of the DoWork procedures, and it all appears to be working fine right now.
Thanks for your time and wisdom
NY
ASKER
havent read your last comment before i accepted your solution, but you were on the right track all along. funny that you should post the last comment 1 minute before i did, being that its late at night and all. ESP like
Glad it all worked out.
I generally check my e-mail and this site before I get ready to go to sleep and see if any one posted any questions that I could help with.;=)
I generally check my e-mail and this site before I get ready to go to sleep and see if any one posted any questions that I could help with.;=)
ASKER
{"Object reference not set to an instance of an object."}
WHY OH WHY???