こんにちは、終了までに約 2 時間かかるループがあります。インターネットからデータを取得してデータベースに入力するので、それを 4 つのスレッドに分割したいと考えています。応用。
ここに、1 つのデータベースを 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