您好,我有一个持续约 2 小时的循环完成,它从 Internet 获取数据并将其填充到数据库中,所以我想将其划分为 4 个线程,我正在尝试做一些测试,然后再把它放到我的应用。
所以这是我写的一个示例,用 4 个线程填充一个数据库,我收到这个错误“BindingSource 不能是它自己的数据源”
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