Question

I am working on windows form application : in my form am filling my datagrid view in some interval.so some time my application getting stuck..so i used back ground worker and timer
in back ground worker i am calling my function to fill the my data grid view.i set Timer Interval as 10000. in background worker i given code like this:

Private Sub BackgroundWorker1_DoWork
Call Fetch_Info()
End Sub

in Timer click event i given code like thise:

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

my Fetch_Info() function like this:

 Dim cnt As Integer
            Dim tbarcodedgv As String
            Dim totaltbarcode As String
            cnt = DGVall.RowCount
 Dim tbar As String
            Dim locTable As New DataTable
            locTable.Columns.Add("carid", GetType(String))
            If cnt > 0 Then
                For i = 0 To cnt - 2
                  tbarcodedgv = DGVall.Rows(i).Cells(0).Value
 locTable.Rows.Add(tbarcodedgv)
                Next
            End If
Dim flag As Boolean = False
            Dim dcnt As Integer = DGVall.RowCount
            Dim trid As Integer
            Dim tbarcode As String
            Dim keyloc As String
            Dim cmd23 As New SqlCommand("IBS_fetchrequested", con.connect)
            cmd23.CommandType = CommandType.StoredProcedure
            cmd23.Parameters.Add("@tid", SqlDbType.Int).Value = tid
            If cnt > 1 Then
                Dim tvp1 As SqlParameter = cmd23.Parameters.AddWithValue("@Tbaroced", locTable)
                tvp1.SqlDbType = SqlDbType.Structured
                tvp1.TypeName = "dbo.TBarcode"
            End If


            dr = cmd23.ExecuteReader
            While dr.Read
                flag = False
                tbarcode = dr("TBarcode")
If flag = False Then
                    If dr("keyloc") Is DBNull.Value Then
                        keyloc = ""
                    Else
                        keyloc = dr("keyloc")
                    End If

                    Dim row0 As String() = {tbarcode, keyloc, "", "Release"}
                    DGVall.Rows.Add(row0)
                    AxWindowsMediaPlayer1.URL = "C:\Beep.mp3"
                End If
            End While
            dr.Close()
            con.disconnect()
Was it helpful?

Solution

While your background worker runs in another thread than your GUI you are manipulating the Datagridview that's running in the GUI's thread. This should usually not work at all but it is probably the reason, why your GUI hangs while the BGW is running.

Try splitting the work: The time consuming fetching of data from the database is carried out in the Backgroundworker's DoWork event handler and you set the results as the e.Result value of the EventArgs variable in the DoWork function.

Then you handle the Backgroundworker's RunWorkerCompleted event and there you quickly update your datagridview with the results you set in the DoWork method. That way your GUI has nothing to do with the actual time consuming task and will only be affected by the quick update of your datagridview.

The code example for this is:

Public Class Form1
Private WithEvents LazyBGW As New System.ComponentModel.BackgroundWorker

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'This code runs in the UI-Thread
    LazyBGW.RunWorkerAsync()
End Sub

Private Sub LazyBGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles LazyBGW.DoWork
    'This code runs in the BGW-Thread
    Dim a As Integer = 0
    For i = 1 To 5
        a += 1
        'I'm a lazy worker, so after this hard work I need to...
        Threading.Thread.Sleep(1000) 'This locks up the BGW-Thread, not the UI-thread
    Next
    'Work is done, put results in the eventargs-variable for further processing
    e.Result = a
End Sub

Private Sub LazyBGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LazyBGW.RunWorkerCompleted
    'This code runs in the UI-Thread
    Dim results As Integer = CInt(e.Result) 'e.Result contains whatever you put into it in the DoWork() method
    MessageBox.Show("Finally the worker is done and our result is: " & results.ToString)
End Sub
End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top