开发者

background worker current status

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 &开发者_如何学C; 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜