Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy this difference between foreach vs Parallel.ForEach?
    text
    copied!<p>Can anyone explain to me in simple langauage why I get a file about 65 k when using foreach and more then 3 GB when using Parallel.ForEach?</p> <p>The code for the foreach:</p> <pre><code>// start node xml document var logItems = new XElement("log", new XAttribute("start", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"))); var products = new ProductLogic().SelectProducts(); var productGroupLogic = new ProductGroupLogic(); var productOptionLogic = new ProductOptionLogic(); // loop through all products foreach (var product in products) { // is in a specific group var id = Convert.ToInt32(product["ProductID"]); var isInGroup = productGroupLogic.GetProductGroups(new int[] { id }.ToList(), groupId).Count &gt; 0; // get product stock per option var productSizes = productOptionLogic.GetProductStockByProductId(id).ToList(); // any stock available var stock = productSizes.Sum(ps =&gt; ps.Stock); var hasStock = stock &gt; 0; // get webpage for this product var productUrl = string.Format(url, id); var htmlPage = Html.Page.GetWebPage(productUrl); // check if there is anything to log var addToLog = false; XElement sizeElements = null; // if has no stock or in group if (!hasStock || isInGroupNew) { // page shows =&gt; not ok =&gt; LOG! if (!htmlPage.NotFound) addToLog = true; } // if page is ok if (htmlPage.IsOk) { sizeElements = GetSizeElements(htmlPage.Html, productSizes); addToLog = sizeElements != null; } if (addToLog) logItems.Add(CreateElement(productUrl, htmlPage, stock, isInGroup, sizeElements)); } // save var xDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("log", logItems)); xDocument.Save(fileName); </code></pre> <p>Use of the parallel code is a minor change, just replaced the foreach with Parallel.ForEach:</p> <pre><code>// loop through all products Parallel.ForEach(products, product =&gt; { ... code ... }; </code></pre> <p>The methods GetSizeElements and CreateElements are both static.</p> <p><strong>update1</strong> I made the methods GetSizeElements and CreateElements threadsafe with a lock, also doesn't help.</p> <p><strong>update2</strong> I get answer to solve the problem. That's nice and fine. But I would like to get some more insigths on why this codes creates a file that is so much bigger then the foreach solutions. I am trying get some more sense in how the code is working when using threads. That way I get more insight and can I learn to avoid the pitfalls.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload