Up until yesterday, I did not know who the heck the BackGround worker is, nor what exactly he's supposed to be doing... so say HELLo to my new BackGround worker.
.
In my project, after scanning the barcode, I need to check if the SerialNumber exist in the SQL DB, if it is then display the data in the DataGridView , next call up the keypad and enter some number. Since checking the DB does takes a few seconds, my display would freeze up before the keypad can appear. So I thought, it would be nice if I can enter my number while its doing the check.
To avoid the problem of cross-threading call, I added a BackGroundWorker to my form, start calling it with
dgvData.DataSource = Nothing
dgvData.Rows.Clear()
BGW1.RunWorkerAsync() 'Using BGW to check if Serial exist
This worker has 3 events associated with it: DoWork, ProgressChanged and RunWorkerCompleted. The DoWork will do the heavy lifting and the Progress or Completed will allow you to set the DataGridView from the original UI thread!!
Here is the code:
Private Sub BGW1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BGW1.DoWork
If SQL.HasConnection = True Then
SQL.RunQuery("SELECT * FROM TB01_DP_TestData WHERE [SerialNumber] = '" & StaSerialNumber & "'")
End If
End Sub
Private Sub BGW1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BGW1.ProgressChanged
End Sub
Private Sub BGW1_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
If SQL.DS.Tables.Count > 0 Then 'Returning from the call
If SQL.DS.Tables(0).Rows.Count > 0 Then 'IF SerialNumber exist
dgvData.DataSource = SQL.DS.Tables(0)
'Label9.Text = SQL.DS.Tables(0).Rows(0).Item(2)
Else
'MsgBox("Not FOund")
End If
End If
End Sub