但是,如果您希望这些备份独立工作(不必等待所有学校都完成),则主要使用您当前的模式。然后我可以玩一些像这样的想法
private ConcurrentDictionary<string,bool> _dict = new ConcurrentDictionary<string,bool>();
...
using (DataTable schools = new DataTable { TableName = "Schools" })
{
schools.ReadXml(AppSettings.Default.SettingsPath);
try
{
Parallel.ForEach(schools.AsEnumerable(), _opts, row =>
{
var ipAddress = row.Field<string>("IPAddress");
// check if there is an ip registered and if its processing
if (_dict.TryGetValue(ipAddress, out processing) && processing)
return;
// its not processing ,so update it
_dict.AddOrUpdate(ipAddress, true, (s, b) => true);
SchoolFunctions.Backup(ipAddress , row.Field<string>("Name"));
// when we are done update processing to false
_dict.AddOrUpdate(ipAddress, false, (s, b) => false);
}
}
}
注意
,有很多方法可以做到这一点,但这看起来很有希望。也未测试
另外,你也可以
TryAdd
TryRemove
忽略值
if (!_dict.TryAdd(ipAddress, false))
return;
SchoolFunctions.Backup(ipAddress , row.Field<string>("Name"));
_dict.TryRemove(ipAddress,out _);