Note that there are some explanatory texts on larger screens.

plurals
  1. PO'List.Contains()' causing 100% CPU usage indefinitely
    primarykey
    data
    text
    <p>While working on a project today I ran into this odd behavior in .NET.</p> <p>I'm grabbing a list of file names and adding them to an object. However I need to filter the incoming names against a known list of "bad" names which led me to do this. (I'm by no means an advanced C# coder, still just learning it.) However, because it causes the <strong>aspnet_wp.exe</strong> process to run at 100% indefinitely, I assume that I'm doing something wrong with the language.</p> <p>Here's my code for reference:</p> <pre><code>List&lt;string&gt; localFiles = new List&lt;string&gt;(); // I was worried the object was causing the issue so dumbed it down to this with no changed. string path = "//&lt;file share&gt;/&lt;dir&gt;/"; // As I commented below yes, it's slow when it works but it's clearly not working when using the if(); List&lt;string&gt; omitNames = new List&lt;string&gt;(); omitNames.Add("Thumbs.db"); // This is the only item in the list when it breaks also. FileInfo[] localFileList = new DirectoryInfo(path).GetFiles(); foreach ( FileInfo item in localFileList ) { if(!omitNames.Contains(item.Name)) { localFiles.Add(path + itemName); } } </code></pre> <p>Can anyone explain why this code runs as an infinite loop? It looks like it really shouldn't. Also, I realize that using a <code>List</code> may not be the best approach. Is there another way to implement this cleanly?</p> <p>Commenting out the <code>if(!){}</code> for <code>omitNames</code> allows the code to run properly. (Though obviously the results are not filtered.)</p> <p><strong>UPDATE:</strong> It was requested to put this in a console app which you'll find below. However it works perfectly. Another thing is that someone suggested that I try to simply compare it against a string. But the exact same thing happens if it's changed to this:</p> <pre><code>if(item.Name != "Thumbs.db") { localFiles.Add(path + itemName); } </code></pre> <p>Console App (which works):</p> <pre><code>using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace LoopKiller { class Program { static void Main(string[] args) { List&lt;string&gt; omitNames = new List&lt;string&gt;(); List&lt;string&gt; localFiles = new List&lt;string&gt;(); omitNames.Add("Thumbs.db"); FileInfo[] localFileList = new DirectoryInfo("c:/test/").GetFiles(); foreach (FileInfo item in localFileList) { if (!omitNames.Contains(item.Name)) { Console.WriteLine("Adding " + item.Name + " to localFiles."); localFiles.Add(item.Name); Console.WriteLine("Item added to localFiles."); } } foreach (string item in localFiles) { Console.WriteLine(item); } Console.ReadLine(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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