Hello I have two methods which work on the same data. Method LoadData() loads/reloads data in memory from file and it is called very rarely (only when files are updated). Method GetData() gets data from memory and it is called from many different threads very often.
I want to achieve following behavior:
When execution of LoadData() begins, block new threads which tries to execute GetData(), then wait until threads, which currently executes GetData() to finish its execution, then continue execute LoadData() and when it is finished unblock the threads which previously were blocked. That's it.
private Object _LoadDataLock;
public void LoadData()
lock (_LoadDataLock) {
{
// (1.) Block new executions of GetData();
// (2.) Wait threads currently executing GetData() to complete its execution.
// (3.) Do some work ..... Loading from files.
// (4.) Unblock blocked in (1.) threads.
}
}
public IDictionary GetData(string key1, string key2)
{
//Do something....
retrn data;
}
How can I achieve that behavior using C#?
Thanks. Ivan