/// <summary> /// 执行批量操作 /// </summary> /// <typeparam name="T">批量操作项类型</typeparam> /// <param name="request">执行批量操作请求数据列表</param> /// <param name="doAction">需要对每个数据项执行的操作</param> /// <param name="exceptionHandler">异常处理器</param> /// <returns>批量操作结果</returns> private static BatchActionResult <T> InnerDoGroupBatchAction <T>(IEnumerable <IGrouping <int, BatchActionItem <T> > > request, Action <T> doAction, Action <Exception> exceptionHandler) { if (request == null || request.Count() == 0) { return(new BatchActionResult <T>(0)); } var result = new BatchActionResult <T>(request.SelectMany(s => s).Count()); //取得主线程的ServiceContext var currentContext = ServiceContext.Current; List <Task> tasks = new List <Task>(); request.ForEach(group => { var g = group; //为每一个组开启一个新的线程,减少线程数量。 var task = Task.Factory.StartNew(c => { //将主线程的ServiceContext附加到当前的ServiceContext ServiceContext.Current.Attach((IContext)c); g.ForEach(item => { try { doAction(item.Data); lock (result.SuccessList) { result.SuccessList.Add(item); } } catch (Exception exp) { lock (result.FaultList) { result.FaultList.Add(new FaultTask <BatchActionItem <T> >(item, exp)); } if (exceptionHandler != null) { exceptionHandler(exp); } } }); }, currentContext); tasks.Add(task); }); //阻塞,直到所有任务完成 Task.WaitAll(tasks.ToArray()); return(result); }
/// <summary> /// 执行批量操作 /// </summary> /// <typeparam name="T">批量操作项类型</typeparam> /// <param name="request">执行批量操作请求数据列表</param> /// <param name="doAction">需要对每个数据项执行的操作</param> /// <param name="exceptionHandler">异常处理器</param> /// <returns>批量操作结果</returns> public static BatchActionResult <T> DoBatchAction <T>(List <BatchActionItem <T> > request, Action <T> doAction, Action <Exception> exceptionHandler) { if (request == null || request.Count == 0) { return(new BatchActionResult <T>(0)); } var result = new BatchActionResult <T>(request.Count); //取得主线程的ServiceContext var currentContext = ServiceContext.Current; List <Task> tasks = new List <Task>(); request.ForEach(r => { var item = r; var task = Task.Factory.StartNew(c => { try { //将主线程的ServiceContext附加到当前的ServiceContext ServiceContext.Current.Attach((IContext)c); doAction(item.Data); lock (result.SuccessList) { result.SuccessList.Add(item); } } catch (Exception exp) { lock (result.FaultList) { result.FaultList.Add(new FaultTask <BatchActionItem <T> >(item, exp)); } if (exceptionHandler != null) { exceptionHandler(exp); } } }, currentContext); tasks.Add(task); }); //阻塞,直到所有任务完成 Task.WaitAll(tasks.ToArray()); return(result); }