-
Feb 7th, 2014, 04:56 PM
#1
Thread Starter
Lively Member
BackgroundWorker is currently busy and cannot run multiple tasks concurrently
I updated a vb.net project from vs 2005 to vs2013 and i get this error message BackgroundWorker is currently busy and cannot run multiple tasks concurrently
and this is my code
Code:
to get the clicked button and set the sql statment
Dim newfrm = DirectCast(sender, DevComponents.DotNetBar.ButtonItem)
Select Case newfrm.Name
Case "showallbtn"
showallbtn.Checked = True
showusergrid.PrimaryGrid.Caption.Text = "Clients & Fournisseur"
xxx = "SELECT * FROM clients "
Case "showclientbtn"
xxx = "SELECT * FROM [clients] WHERE type= 'Client' "
showclientbtn.Checked = True
showusergrid.PrimaryGrid.Caption.Text = "Clients"
Case "showfournbtn"
xxx = "SELECT * FROM clients WHERE type='Fournisseur' "
showfournbtn.Checked = True
showusergrid.PrimaryGrid.Caption.Text = "Fournisseur"
End Select
Code:
and this code is the backgroundworker code
Private Sub showallworker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles showallworker.DoWork
If connection.State = ConnectionState.Open Then connection.Close()
connection.Open()
Dim myadapter As New OleDb.OleDbDataAdapter(xxx, connection)
Dim ds As New DataSet
myadapter.Fill(ds)
myadapter.Dispose()
Dim tbl As DataTable = ds.Tables(0)
Dim panel As GridPanel = showusergrid.PrimaryGrid
panel.Rows.Clear()
showusergrid.BeginUpdate()
For i As Integer = 0 To (tbl.Rows.Count.ToString - 1)
Dim civility = IIf(tbl.Rows(i).Item("civil") <> String.Empty, Space(1) & "(" & tbl.Rows(i).Item("civil") & ")", "")
panel.Rows.Add(New GridRow(tbl.Rows(i).Item("id"), tbl.Rows(i).Item("type"), tbl.Rows(i).Item("nom") & civility, tbl.Rows(i).Item("adresse"), tbl.Rows(i).Item("gsm"), tbl.Rows(i).Item("fax"), tbl.Rows(i).Item("bureau"), tbl.Rows(i).Item("domicile"), tbl.Rows(i).Item("postale")))
Next i
showusergrid.EndUpdate()
connection.Close()
End Sub
I used a special backgroundworker for every button but i got the same error
-
Feb 7th, 2014, 04:59 PM
#2
Re: BackgroundWorker is currently busy and cannot run multiple tasks concurrently
Please dont post in bold. A special back ground worker? No such thing. Please dont just post code and expect us to search through it and find the issue. Which line throws the exception?
-
Feb 7th, 2014, 05:43 PM
#3
Re: BackgroundWorker is currently busy and cannot run multiple tasks concurrently
I think the OP meant a different BGW for each button. I also suspect that the error wasn't on any of those lines.
It looks like you are updating the UI from the BGW, which shouldn't even be possible unless you turned CheckForIllegalCrossThreads OFF (I may have that name wrong off a bit). But there's certainly something missing because nothing in those snippets launches a BGW, so it is hard to say why it would be called twice, special or not.
My usual boring signature: Nothing
-
Feb 7th, 2014, 08:25 PM
#4
Re: BackgroundWorker is currently busy and cannot run multiple tasks concurrently
As Shaggy suggests, you are rather misusing the BackgroundWorker. As the name suggests, it is for doing background work. You appear to be accessing the UI in your DoWork event handler, which is the on thing you must NOT do with a BackgroundWorker. Anything to do with the UI is inherently foreground work, so should absolutely NOT be done in the DoWork event handler and often can't be. It appears that what you want to do is retrieve some data and then display it. The retrieving part is something that you can do in the DoWork event handler and you may even be able to process the data and package it such that it is ready to be displayed, but you MUST NOT display it. You can pass the data via the e.Result property to the RunWorkerCompleted event handler, which is executed on the UI thread, and then update the UI there.
As for the issue you stated, a single BackgroundWorker can only do one thing at a time. While it's working, its IsBusy property will be True. As long as IsBusy is True, you cannot call RunWorkerAsync again. If you genuinely need to run multiple background tasks simultaneously, which I doubt you actually do, then one option is to follow the CodeBank link in my signature below and check out my BackgroundMultiWorker class.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|