0
我刚刚在VB.net中学习了一些多线程的基础知识,最近我遇到了处理大型记录列表并逐个将它们插入到SQL数据库中的问题。 我有代码看起来是这样的:多线程处理问题
Private Sub btnLoadNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadNow.Click
Dim autoLoad1 As New Thread(AddressOf AutoLoad)
autoLoad1.Start()
Dim autoLoad2 As New Thread(AddressOf AutoLoad)
autoLoad2.Start()
Dim autoLoad3 As New Thread(AddressOf AutoLoad)
autoLoad3.Start()
Dim autoLoad4 As New Thread(AddressOf AutoLoad)
autoLoad4.Start()
Dim autoLoad5 As New Thread(AddressOf AutoLoad)
autoLoad5.Start()
Dim autoLoad6 As New Thread(AddressOf AutoLoad)
autoLoad6.Start()
Dim autoLoad7 As New Thread(AddressOf AutoLoad)
autoLoad7.Start()
Dim autoLoad8 As New Thread(AddressOf AutoLoad)
autoLoad8.Start()
Dim autoLoad9 As New Thread(AddressOf AutoLoad)
autoLoad9.Start()
End Sub
Private Sub AutoLoad()
for each Item as Record In ItemLists
Process Codes
Next
End Sub
的项目列表是一个全局列表我从数据库中检索,一切看起来好像没什么问题,但是当我运行这个程序,我发现线程插入一个记录9次进入数据库(我总共有9个线程),这让我想我可能需要专门为每个线程分配1/9的列表?是否有另一种方式来做到这一点,如果不需要分割列表,我如何分割它然后分配?任何帮助将受到欢迎。
我确实在循环代码中有synclock。你介意给我一个使用共享变量的例子吗?谢谢。 – miketonny 2011-03-18 03:59:01