Link to home
Start Free TrialLog in
Avatar of newyuppie
newyuppieFlag for Ecuador

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
Avatar of newyuppie
newyuppie
Flag of Ecuador image

ASKER

App.Doevents raises

{"Object reference not set to an instance of an object."}

WHY OH WHY???
Avatar of Fernando Soto
Can you post the code for the BacgroundWorker?
ok here it goes

Private Sub BackWorkerLayoutCombos_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackWorkerLayoutCombos.DoWork
        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.Parameters("PCategoria").Value = li.Text
            MySqlCommandStorProc.ExecuteNonQuery()
            Me.ProporcionesMySqlDataAdapter.Fill(dt)
            DataTableList.Add(dt)
        Next li
    End Sub

    Private Sub BackWorkerLayoutCombos_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackWorkerLayoutCombos.RunWorkerCompleted

        For Each dt As DataTable In DataTableList
            Dim StoredLi As ListItem = CType(GenericList.Item(DataTableList.IndexOf(dt)), ListItem)
            Dim ctl As ComboBox = DirectCast(StoredLi.Control, ComboBox)
            ctl.BeginUpdate()
            ctl.DataSource = dt
            ctl.Tag = dt.Rows(0).Item("Tipo_Variable").ToString
            ctl.EndUpdate()

            Dim DefaultIndex As Integer = -1
            Dim CategoriaField As String = StoredLi.Text
            Dim CategoriaFieldMapping As String = StoredLi.SolicitantesColumnMapping
            Dim DefaultSubCategoria As String = Nothing

            For i As Integer = 0 To (dt.Rows.Count - 1)
                If (dt.Rows(i).Item("IsDefault") IsNot System.DBNull.Value) Then
                    If dt.Rows(i).Item("IsDefault") = 1 Then
                        DefaultIndex = i
                        DefaultSubCategoria = dt.Rows(i).Item("Subcategoria")
                        Exit For
                    End If
                End If
            Next i

            If MySQLDataset.solicitantes.Columns.Contains(CategoriaFieldMapping) Then
                MySQLDataset.solicitantes.Columns(CategoriaFieldMapping).DefaultValue = DefaultSubCategoria
            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_DoWork and

    Console.WriteLine("RunWorkerCompleted Entered")

At the beginning of BackWorkerLayoutCombos_RunWorkerCompleted to see if it gets to those points.

   
it gets to the DoWork done, but never enters the RunWorkerCompleted...
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...
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
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.;=)