I have to store and return data on multitasking requests. If data is missing, I schedule loading and return null. I'm using some nubie scheduling by saving current jobs in a list. Also I need data request to be locked during writing. .NET 4.0
How can I schedule and lock properly?
private ConcurrentDictionary<string, IDataSet> data;
private List<string> loadingNow;
public IDataSet GetData(string dataId)
{
if (loadingNow.Contains(dataId))
return null; // Currently loading. Return null
if (data.ContainsKey(dataId))
return data[dataId]; // Return data
// Schedule loading async. Return null.
loadingNow.Add(dataId);
dataIoAsync.LoadDataAsync(dataParams);
return null;
}
private void DataIoAsync_DataFileLoaded(object sender, DataFileLoadedAsyncEventArgs e)
{
loadingNow.Remove(e.DataId);
data.TryAdd(e.DataId, e.DataSet);
OnDataFileLoaded(e.DataId);
}
EDIT: Dictionary replaced with ConcurrentDictionary