backgroundworker Question

G

Guest

We have been using multi-threading in our application in 2003 with good
results. When we migrated to 2005 our old code blew up, so we rewrote our
code using the backgroundworker. It seemed to easy to be true. When we run
our application now it seems that the screens are running synchronously and
not Async. We usually have 3 to 4 backgroundworkers going at the same time
inside the activated event for the form. See the code below to show how we
are using the worker:

Private QueueWorker As New BackgroundWorker
Delegate Sub QueueDelegate()

Private Sub LoadInitialQueue(ByVal sender As Object, ByVal e As
DoWorkEventArgs)
Try
Dim ReturnAsync As IAsyncResult = BeginInvoke(New
QueueDelegate(AddressOf SetupQueue))
EndInvoke(ReturnAsync)
Catch exp As Exception
MessageBox.Show(exp.Message.ToString)
End Try
End Sub

Public Sub SetupQueue()
Try
QueueFlex.Redraw = False

dsNotifications.Clear()
dsNotifications.Reset()

Dim parm() As Object = {Main.oProperties.UsersBankLoanTeamID, 0}
DBCalls.AccessDataLayer.FillDataset("SelUserNotifications",
Main.oProperties.ConnectionString, dsNotifications, New String() {"queue"},
parm)
QueueFlex.DataSource = dsNotifications.Tables("queue")

FormatQueue()

QueueFlex.Redraw = True
Catch exp As Exception
MessageBox.Show(exp.Message.ToString)
End Try
End Sub

Do we have to use BeginInvoke & EndInvoke with this new component because
that seems like the bottleneck. If we do still need to use them what can I
do differently that would make the UI more responsive. It just seems that we
lost the UI responsiveness when we moved over to the backgroundworker. it
looks like it gets the data from the sql server async but it doesn't populate
the controls async so it looks sync.

I don't know any help would be appreciated.
 
G

Guest

You should be using the worker as follows (C# code):

// 'info' is a struct containing multiple parameters to use in bg processing
bgWorker.RunWorkerAsync(info);

private void bgAddTime_DoWork(object sender, DoWorkEventArgs e)
{
// Do the work here on the object passed
}

private void bgAddTime_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
// Do some stuff here now that the worker is done
}


Note that the worker completed section can be empty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top