Parallel.ForEach 在最终迭代期间冻结

本文关键字:冻结 迭代 ForEach Parallel | 更新日期: 2023-09-27 17:57:06

这是我第一次与Parrllel.ForEach一起工作,我遇到了问题。这是一种耻辱,因为我真的需要这个程序的表现,而且我得到了它。

我会说在它的最后一次迭代中,我的ForEach正在冻结。这不会公开任何错误消息。ConsoleWriteLine()停了下来。当我在调试器中暂停时,Parallel.ForEach的范围会突出显示。

   using (DBDataContext db = DataContextFactory.GetThreadScopedDataContext<DBDataContext>())
            {
                products = (from r in db.tbl_products
                           where r.date >= _dateCheck
                           select r
                ).ToList();
                int rcount = products.Count();
                Console.WriteLine("Total products: {0}", rcount);
                Parallel.ForEach(products, new ParallelOptions { MaxDegreeOfParallelism = 8 }, product =>
                {
                    DBDataContext db2 = DataContextFactory.GetThreadScopedDataContext<DBDataContext>();

                    CheckInventory(product, db2);

                });
                Console.WriteLine("Ended Inventory Check {0}", DateTime.Now);
            }

CheckInventory() 方法的最后一个阶段是创建匹配记录

  private static void AddNewInventoryMatch(tbl_product product, tbl_inventory item, int matchType)
        {

            DBDataContext db = DataContextFactory.GetThreadScopedDataContext<DBDataContext>("4");
            tbl_inventoryMatch newMatch = new tbl_inventoryMatch();
          //... Add records

            db.tbl_inventoryMatches.InsertOnSubmit(newMatch);
            //  db.SubmitChanges();

        }

我已经在网上寻找可能导致这种情况的原因,但我没有运气。这是一个简单的控制台应用程序,每晚运行一次。

同样在它锁定 VS 之后,除非我重新启动 VS,否则无法编译项目。我收到此错误消息。

Error   11  Could not copy "obj'x86'Debug'Inventory-Match.exe" to "bin'Debug'Inventory-Match.exe". Exceeded retry count of 10. Failed.  Inventory-Match
Error   12  Unable to copy file "obj'x86'Debug'Inventory-Match.exe" to "bin'Debug'Inventory-Match.exe". The process cannot access the file 'bin'Debug'Inventory-Match.exe' because it is being used by another process. Inventory-Match

链接:数据上下文工厂 = http://weblog.west-wind.com/posts/2008/Feb/05/Linq-to-SQL-DataContext-Lifetime-Management

Parallel.ForEach 在最终迭代期间冻结

我很久以前就遇到了同样的问题,请尝试使用GetDataContext而不是GetThreadScopedDataContext