views:

18

answers:

1

I am using background threads to run intense search processes in order to allow the UI to be fully accessible. After creating each bgw I update a datagridview which shows the different threads and current status. however once they complete, I have no way or at least I dont know of a way to update the status on the datagridview specific to each backgroundworker.

 Try
        bgw.RunWorkerAsync()
        queuelist.Enqueue(bgw)
        If Not Thread.CurrentThread.Name = "Main Thread" Then
            Dim record As String() = {jobNum, clientProj & jobNum, jobStartTime,   bgw.IsBusy.ToString}
            DataGridView1.Rows.Add(record)
        End If

    Catch ex As Exception
        MessageBox.Show("An Error Occured:" & vbNewLine & ex.Message)
    End Try

this sets the datagridviewer once the threads begin, but once it ends I dont know how to update or know which thread ended. I tried putting them into a queue but I cannot identify a specific worker when i dequeue.

any ideas

+1  A: 

I really don't understand why you'd make implementation details like background workers visible on the user interface. Well, some code. It doesn't make sense to use a Queue, threads do not end in any predictable order. Let's do a list:

Dim workerList As New List(Of BackgroundWorker)

You want to take advantage of the RunWorkerCompleted event raised by BGW to know when the job is done. So use AddHandler:

    Dim bgw As New BackgroundWorker
    AddHandler bgw, AddressOf DoSomeWork
    AddHandler bgw, AddressOf WorkDone
    workerList.Add(bgw)
    '' Do something with the grid
    ''...
    bgw.RunWorkerAsync()

And the event handler can look like this:

Private Sub WorkDone(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    If e.Error IsNot Nothing Then Throw e.Error
    Dim bgw = DirectCast(sender, BackgroundWorker)
    workerList.Remove(bgw)
    '' Do something with the grid
End Sub
Hans Passant
Thanks for the response and btw, I need to display each process running on the UI as the current state of each job like this: jobId jobname starttime status
vbNewbie