I'm helping spruce up an application, and it's full of concurrency issues. I'm attempting to work through them, and came across a section that's using DataTables.
The datatable itself is static, shared across many threads.
I know that using dt.Select("...") itself needs a lock statement, or you'll have issues when adding/removing rows to the data table. However, when that call returns, you have an array of DataRow objects.
I'd obviously lock those rows if I was to update them, but if I'm just reading them, do they need locking?
Basically, given that elsewhere we're adding new rows and potentially updating existing rows, which of these is correct:
lock (dtLock)
{
DataRow[] rows = dt.Select("...");
}
foreach(DataRow dr in rows)
{
// read statements only
}
or
lock (dtLock)
{
DataRow[] rows = dt.Select("...");
foreach(DataRow dr in rows)
{
// read statements only
}
}