I have created a little windows service which should delete all occurrences of a certain file name in certain folders.
All this code runs in the elapsed-handler of the timer (intervall=10s).
When the service is running I can recognize a CPU increase up to 20% used by that service, so I examined my code, put some trace commands in it and found out that executing the handler took about 3-4 seconds for nothing.
I narrowed it down to the following piece of code: allReporterFiles.Count().
It's calling the method Count() of this IEnumerable and this call takes 3-4 seconds.
My project is setup for .NET 4.7.2. Is this a framework bug or what?
var files1 = Directory.EnumerateFiles(dirSwReporter, swReporterFileName, SearchOption.AllDirectories);
var files2 = Directory.EnumerateFiles(dirSwReporter2, swReporterFileName, SearchOption.AllDirectories);
var allReporterFiles = files1.Union(files2);
var sw = Stopwatch.StartNew();
var fileCount = allReporterFiles.Count(); // <--- takes ~3.5 seconds
sw.Stop();
Trace.WriteLine($"KillChromeSoftwareReporterTool completed in: {sw.Elapsed.TotalMilliseconds}ms or {sw.Elapsed.TotalSeconds}sec");