Why does the following code at random times write null values to the DataRow object?
ConcurrentDictionary<long, DataRow> dRowDict = new ConcurrentDictionary<long, DataRow>();
foreach (long klucz in data.Keys)
{
DataRow row = table.NewRow();
dRowDict.TryAdd(klucz, row);
}
Parallel.ForEach(data.Keys, klucz =>
{
Dane prz = data[klucz] as Dane;
DataRow dr = dRowDict[klucz];
foreach (PropertyDescriptor prop in llProp)
{
dr[prop.Name] = prop.GetValue(prz) ?? DBNull.Value;
}
});
foreach (DataRow dRow in dRowDict.Values)
{
table.Rows.Add(dRow);
if (dRow["FILED"] == DBNull.Value)
{
MessageBox.Show("ERROR...NULL VALUE"); //why this happen?
}
}
A plain for loop does not cause this problem - why, where is my bug?
My question is - can I modify the properties of any object inside a parallel loop?
Parallel.ForEach(myData.AsEnumerable()..., value =>
{
Object x = new Object(); // or x = dict[key];
x.A = ...;
x.B = ...; //Can I do this and is it safe?
}