0

I'm using windows form application.and it get slow down on click of any button. I'm using 2 background worker( first is doing some sorting data and other one is continuously getting data from server through web service) not able to understand whats going on, Previously it is working fine with only one background worker but when adding second worker the application performance slow down and some times it goes in "Not Responding State"(on double click on form). Any one have any idea why it is behaving like this ? if i stop second worker then it worker fine again.

Here is my code for second BGW

If Not bwProcessData.IsBusy Then
   bwProcessData.RunWorkerAsync()
End If

Private Sub bwProcessData_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwProcessData.DoWork

    While get_all_server_data
        Sync_the_data()
        Application.DoEvents()
    End While

        While IsFutureRecords Or IsPastRecords
            If IsFutureRecords Then
                While IsFutureRecords
                  '' API call for all future date records
                  '' And the update my database
                  '' if no record found then IsFutureRecords = false
                End While
            ElseIf IsPastRecords Then
                While IsPastRecords
                    '' API call for all Past date records
                    '' And the update my database
                    ''  if no record found then IsPastRecords = false
            End While
            End If
        End While
   End Sub


Private Sub bwProcessData_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwProcessData.RunWorkerCompleted         
            If e.Cancelled Then
            Else
                Dim run_syn_complete_2 As UpdateUIFrom_Background_2 = New UpdateUIFrom_Background_2(AddressOf run_sync_complete_2)
                Dim str_value As String = "Background worker 2"
                Me.Invoke(run_syn_complete_2, str_value)
            End If
End Sub

Public Sub run_sync_complete_2()
    '' Update UI accordingly
End Sub
Rahul Shirphule
  • 989
  • 3
  • 14
  • 36
  • There shouldn't be any problem with the number of BGWs if you use them properly. Please, post your code. – varocarbas Oct 03 '13 at 07:12
  • yes i'm agree too with no of BGWs use in application. But my app is slowing down with second BGWs which is continuously fetching data and update my database file. might be this cause. i'll put my code but it too lengthy to post. but when stop second BGWs it works fine ..! – Rahul Shirphule Oct 03 '13 at 07:50
  • You don't need to write the whole code, just the relevant parts: where both BGW threads are started and a (short) reference to what is done in each them (input conditions, etc.). Also highlight any cross-threading issue; for example GUI thread called from one of the BGW methods. Just help us to get a clear picture of what is happening in your code. – varocarbas Oct 03 '13 at 07:53
  • Hi @varocarbas, i edited my question.please take look. – Rahul Shirphule Oct 03 '13 at 08:51
  • Aand one more thing on my observation my application takes 30-40 % CPU usage when second BGW is running.otherwise it is normal 4-6 % CPU usage. – Rahul Shirphule Oct 03 '13 at 08:58
  • Me.Invoke(run_syn_complete_2, str_value) makes the second BGW thread to be called from the GUI thread. – varocarbas Oct 03 '13 at 08:59
  • Also you shouldn't use Application.DoEvents(): http://stackoverflow.com/questions/5181777/use-of-application-doevents – varocarbas Oct 03 '13 at 09:00
  • Summary: try to remove the Application.DoEvents part and either don't use BGW for the second call (as far as everything is done in the GUI) or call it "properly" as in the first one (secondBGW.RunWorkerAsync()) – varocarbas Oct 03 '13 at 09:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38531/discussion-between-rahul-shirphule-and-varocarbas) – Rahul Shirphule Oct 03 '13 at 09:08

0 Answers0