int limitTimes; int.TryParse(ConfigurationManager.AppSettings["LimitTimes"], out limitTimes); var redoPurchaseEntityList = this.redoPurchaseEntityRepository.FindByExpression(p => p.SendNumber < limitTimes).ToList(); if (redoPurchaseEntityList.Count == 0) { return; } Parallel.ForEach( this.redoPurchaseProvideList, redoPurchase => { var task = new Task(() => redoPurchase.RedoPurchase(redoPurchaseEntityList)); task.StartAndContinueWith(); });
上面代码主要是从数据库中查询出一个表的list集合然后多线程并行查找同一个list,会得到一个错误信息
There is already an open DataReader associated with this Command which must be closed first.
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
因为用的是一个session,导致读取错误多线程不可互用同一个session.
可参考 http://stackoverflow.com/questions/7886260/nhibernate-multi-threading-issue