简体   繁体   中英

Parallel.Foreach and Parallel Execution in C#

I am having a task of scanning all the folders name start with "xyz" parallely. I meant if one folder getting scan same time other one should also getting scan. I don't want one by one scanning.

For that I used Parallel Foreach.

Question is? Is it correct or not? and How to know is it running parallely(to put any message some where)?

Parallel.ForEach(path, currentPath =>
   {
    var output = programReader.GetData(currentPath, durReader.dirPattern);

    foreach (var item in output)
      {
        foreach (var project in item.Name)
           Console.WriteLine(item.serverName + " " + item.serverNumber + " " + fileName);
        }
     }

EDIT:

Is Parallel.Foreach only works on multicore systems or it could work on single core system also to perform show parallelism

Foirst - if you ask a question, make it a question. !! is a good indication it is not a question.

Second, your approach makes little sense. Parallel will go VERY parallel. Good. Bad: you still have only one disc. Result: tasks waiting. It makes no sense to paralellize IO operations over the degree supported by the hardware.

The Parallel extensions split the load per core - although I'm not sure if they take into account hyperthreaded cores.

But, to add another answer to steer you in the right direction:

Don't try and do this in parallel. The disk can only serve one request at a time, so you're likely to just slow everything down as the disk queue is just likely to get bigger.

The only scenario where you might be able to get increased performance is if the location you're scanning is actually a SAN where the storage is distributed and highly replicated.

您可以打印Thread.ManagedThreadId值以查看正在使用的线程。

Parallel.ForEach or Parallel.Execute uses the cores of the processor. So if your processor is having more thn one core they will be used equally to run this thread. Here you are m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM