I have a DataSet that is cached deep down in a business layer object.
Higher up, there's a merge being performed on that object and it very
occassionaly throws a NullReferenceException deep down in the merge
code. I suspect that the DataSet is being edited during the merge by
another thread or its going out of scope (i.e. expiring).
The question is whether I can/should lock that reference object. Would
it help?
For example:
public SomeMethod() {
DataSet moreData = SomeObject.GetNonCachedData();
DataSet blah = SomeObject.GetCachedDataSet();
lock(blah) {
moreData.Merge(blah);
}
}
In this case 'blah' is a local object so it's not a threading issue per
se. I just think that another thread is affecting the cache and since
the DataSet is a reference object, it's causing this method to blow up.
What I'm really looking for is the best way to keep that reference
object from being edited during the merge. Is using that lock going to
help me?