Syncronize write operations in a DataTable

  • Thread starter Thread starter Sebastian Loncar
  • Start date Start date
S

Sebastian Loncar

Hello, what is the best way to syncronize write operations in a DataTable?

In the help is: 'This type is safe for multithreaded read operations. You
must synchronize any write operations'

It exists diferent solutions, but i don'n know what is the best.

Solution 1:
Synclock Row.Table
Synclock Row.Table.Rows.Syncroot
Synclock Row
Row("column") = Value
End Synclock
End Synclock
End Synclock

This is very secure, but it slows my application down.

Solution 2:
Private Shared Waiter As New Threading.AutoResetEvent(True)
Public Shared Sub SetValue(Row as DataRow, Column as String, Value as
Object)
Waiter.WaitOne(1000, False)
Row("column") = Value
Waiter.Set()
End Sub

Solution 3:
Using of System.Threading.ReaderWriterLock, but i down know how to use it
and if this is correkt.

For solution 1: I aks me if the DataTable and the DataRowCollection must
realy lock. But ein read in another newsgroup, that a lock of the
DataRow-Object ist not enougth.
I've to remember that a DataRow.AcceptChanges has to syncronize zu.

Some background informations:
I use in my appliction a global DataSet, lots of reads and writes are
running all the time. But sometimes a NullReferenceExcpetions was thrown
from the DataTAble, if 2 threads writes to the same time. I don't know if
the Error only occurs if 2 Threads write to the SAME row at the SAME time or
some row at the same time. The concerned DataRow changes to a undefined
state, i've to restart the application.
And i ask me why the DataTable is not thread safe :(
 
From what I see, you will need to use a reader writer lock for best
performance. This is primarily because, the Datatable is documented to be
thread safe for read operations, not write.
So, by the virtue of using a reader writer lock, you can have multiple
readers, but only one writer at any instant.
To undertand reader writer locks, you can have a look at the samples for
threading at this folder <<.NET Framework SDK
folder>>\Samples\Technologies\Threading
 
Back
Top