Reputation: 880
Hello i've a loop that last about 2 hours to finish, it gets the data from internet and fills it to a database so i want to divide it to 4 threads, and i am trying to do some testing before i put this to my application.
so here is a sample that i wrote to fill one database by 4 threads and i am getting this error "BindingSource cannot be its own data source"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.DataSource = Me.bindingSource1
GetData("select * from products")
End Sub
Public Sub thread1f0()
For i = 0 To 500
DataGridView1.Item("PD", i).Value = i + 67
DataGridView1.Item("PDP", i).Value = i + 41
DataGridView1.Item("TPD", i).Value = i + 654 + 6 * 13
DataGridView1.Item("TPDP", i).Value = i + 342
Next
End Sub
Public Sub thread1f1()
For i = 501 To 1000
DataGridView1.Item("PD", i).Value = i + 432
DataGridView1.Item("PDP", i).Value = i + 421
DataGridView1.Item("TPD", i).Value = i + 414
DataGridView1.Item("TPDP", i).Value = i + 42 + 4
Next
End Sub
Public Sub thread1f2()
For i = 1001 To 1500
DataGridView1.Item("PD", i).Value = i + 4452
DataGridView1.Item("PDP", i).Value = i + 34
DataGridView1.Item("TPD", i).Value = i + 123
DataGridView1.Item("TPDP", i).Value = i + 44
Next
End Sub
Public Sub thread1f3()
For i = 1501 To 2000
DataGridView1.Item("PD", i).Value = i + 423
DataGridView1.Item("PDP", i).Value = i + 423
DataGridView1.Item("TPD", i).Value = i + 423
DataGridView1.Item("TPDP", i).Value = i + 423 / 2
Next
End Sub
'//////////////////////////////////////////////////////////////////////////////////////////
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
thread0 = New System.Threading.Thread(AddressOf thread1f0)
thread0.Start()
thread1 = New System.Threading.Thread(AddressOf thread1f1)
thread1.Start()
thread2 = New System.Threading.Thread(AddressOf thread1f2)
thread2.Start()
thread3 = New System.Threading.Thread(AddressOf thread1f3)
thread3.Start()
End Sub
Public Sub GetData(ByVal selectCommand As String)
Try
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName
Me.dataAdapter = New OleDbDataAdapter(selectCommand, connectionString)
Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.ColumnHeader)
Catch ex As OleDbException
MessageBox.Show(ex.Message)
End Try
End Sub
Upvotes: 1
Views: 857
Reputation: 81620
Since your DataGridView control is using a DataTable as a DataSource, you need to update the DataTable, not the DataGridView control.
The threads don't seem necessary. Just use one BackgroundWorker thread to not freeze the GUI.
Upvotes: 1