マルチスレッドでのデータテーブルの操作を試みたところ
System.NullReferenceException
System.Data.ConstraintException
System.ArgumentOutOfRangeException
といったエラーがランダムに発生します。
(それぞれのエラーの意味はわかります。)
大雑把なコードは以下のようになっています。
-------------------- ここから --------------------
Private Shared dt As DataTable
Private Sub InitTable()
dt = New DataTable
Dim dc(1) As DataColumn
Dim pk(0) As DataColumn
dc(0) = New DataColumn
dc(0).DataType = GetType(String)
dc(1) = New DataColumn
dc(0).DataType = GetType(Integer)
pk(0) = dc(0)
dt.PrimaryKey = pk
End Sub
Private Sub Execute()
For Each r As DataRow In dt.Rows
Threading.ThreadPool.QueueUserWorkItem _
(New System.Threading.WaitCallback _
(AddressOf UpdateData), _
New UpdateTarget(Convert.ToString(r(0)))
Next
End Sub
Private Shared Sub UpdateData(ByVal obj As Object)
Dim Key As UpdateTarget = CType(obj, UpdateTarget)
Dim r As DataRow = dt.Rows.Find(Key)
'---------- この間でエラーとなる ----------
r.BeginEdit()
r(1) = Convert.ToInt32(r(1))+1
r.EndEdit()
'------------------------------------------
End Sub
Public Class UpdateTarget
Dim _Key As String
Public Property Key() As String
Get
Return _Key
End Get
Set(ByVal Value As String)
_Key = Value
End Set
End Property
Public Sub New(ByVal Key As String)
_Key = Key
End Sub
End Class
-------------------- ここまで --------------------
どのあたりがマズいのかご指摘いただければ幸いです。
よろしくお願いします。
>読み取り操作に対して
を読み落としとしていました。
SyncLockあたりを勉強して出直します。
お騒がせしました。