I have a singleton obj called DataStorage that stores shared data in my application; one of them is a Datatable called myTable, which will be read and written by multiple threads. I have a private object that will be used as a lock in a DataStorage, i.e.
private object lockObj = new object();
I closed the locks to access myTable as such:
private DataTable myTable;
public DataTable MyTable
{
get
{ lock(lockObj) { return myTable; } }
set
{ lock(lockObj) { myTable = value; } }
}
Another object, that is, MyObj will receive this data type, make a selection on it, and then change some value in the extracted DataRow [] from Select. I read that Select is not thread safe, so I wrote my code as follows:
// lock on MyTable
DataTable dt = DataStorage.Instance.MyTable;
lock (MyObjLockObj) // lock object for MyObj class
{
// do a select, then modify value in the returned row
DataRow[] foundRows = dt.Select("some expression");
foundRows[0]["some col"] = 123456;
}
Questions: 1. In general, is this code safe?
DataRow MyObj, MyTable, ? DataTable, DataRow.
DataStorage.Instance.MyTable , Select?
.