views:

158

answers:

3

I have made an application which runs three backgroundworkers simultaneously. It basically updates three datagridviews, which it is doing spot on. My problem is if I press maximizebox button or anywhere in any of the datagridview the program hangs for quite a long time. I am able to use the horizontal scroll but not vertical scrolls. I have tried Backgroundworker_runworkercompleted and it fires as required after threads have updated their respective datagridviews. Is it a normal behaviour or am i doing something wrong any suggestions would be helpful. P.S: I have run the whole program using step method and their is no infinite loop in the code. Thanks in advance Jhon

A: 

It sounds like you are still blocking the UI thread somehow. It may be helpful for you to post some code snippets. Also, what is the CPU utilization of your process? If the cpu usage is high, you may be starving the UI thread somehow.

Justin Ethier
A: 

Thanks Justin, since there's lot of code so am not sure which part to add as there seems to be no problem with the code itself. The backgroundworker has WorkerAllowsCancellation property set as true while WorkerReportsProgress has been set false since it does not need to report progress. However for the sake of experimentation I did try setting it as true to same effect. I also have reduced the code my whole app to a single background thread to no avail. The UI gets blocked only after the grid has been updated, I did try RunWorkerCompleted event too and it does fires up, which means the thread is not getting entangled in the GridFiller function. CPU utilization is minimal since BackGroundWorker basically fetches data from the net. Any help would be much appreciated.

Private Sub MainWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles MainWorker.DoWork
    'myWorker is an instance of a Class Worker which has working functions declared.
    Dim myWorker As New Worker
    Dim Worked As String

    Dim selection As String = ComboBox1.SelectedItem

    If selection = "All" Then
        For i = 1 To ComboBox1.Items.Count - 1
            selection = ComboBox1.Items.Item(i).ToString
            'Run(selection) is a function declared in Class Worker.
            Worked = myWorker.Run(selection)
            Worked = Worked.Replace(".", "")
            'GridFiller is a function which fills up the datagridview.
            GridFiller(Worked)
            Me.Refresh()
        Next
    Else
        Worked = myWorker.Run(selection)
        Worked = Worked.Replace(".", "")
        GridFiller(Worked)
        Me.Refresh()
    End If

End Sub
Jhon
A: 

Okay I have found the solution to my problem, while working out sequential elimination of the perceived trouble spots, I called my datagridview outside of the backgroundworker.dowork event and voila that solved the problem. Moral of the story "NEVER UPDATE A DATA GRID VIEW FROM WITHIN THE BACKGROUNDWORKER THREAD" specially when you don't know what are you doing wrong :). I hope it will help someone in future.

Jhon