我有一个事件,当被解雇时,订阅者执行一些可能需要一段时间才能返回的数据库代码.我想并行执行所有订阅者委托.我正在尝试使用Parallel.Foreach,但我无法得到任何建议吗?
我目前正在尝试沿着这些方向做点什么.
Parallel.ForEach(FacilitiesChanged.GetInvocationList(),某种lambda表达式)
其中FacilitiesChanged是一个事件.
c# parallel-processing events delegates task-parallel-library
我有一个实现单例模式的静态DataLibrary类.
public static FacilityRepository FacilRepo
{
get
{
if (_facilRepo == null)
{
_facilRepo = new FacilityRepository(Authenticated.UserId);
if (Authenticated.FacKey.Length > 0)
{
foreach (var fac in _facilRepo)
fac.IsSelected = (fac.FacilityKey == Authenticated.FacKey);
}
}
return _facilRepo;
}
}
private static FacilityRepository _facilRepo;
Run Code Online (Sandbox Code Playgroud)
当我使用Task.Factory.StartNew从不同的线程访问它时,FacilityReposity会多次重新创建,我该如何避免这种情况.