Parallel .Foreach and parallel execution in C #

I have the task of scanning all folder names using "xyz" in parallel. I meant if one folder receiving the scan at the same time, the other should also receive the scan. I do not want to scan one after another.

For this, I used Parallel Foreach.

Is there a question? Is this right or wrong? and how do you know if it works in parallel (to send a message somewhere)?

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:

Parallel.Foreach only works on multi-core systems or can run on a single-core system also to run show parallelism

+4
source share
4 answers

Foirst - if you ask a question, ask him a question. !! is a good indicator, this is not a question.

Secondly, your approach makes little sense. Parallel will be VERY parallel. Good. Bad: you still have only one drive. Result: task expectations. It makes no sense to paralyze I / O to the extent supported by the hardware.

+3
source

Parallel extensions share the load on the kernel - although I'm not sure if they take into account hyper-threading kernels.

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

Do not try to do this in parallel. A disk can only serve one request at a time, so you probably just slow things down, as the disk queue is likely to become larger.

The only scenario in which you can improve performance is that the location scan is actually a SAN where the storage is distributed and highly replicated.

+1
source

You can print the value of Thread.ManagedThreadId to find out which threads are being used.

0
source

Parallel.ForEach or Parallel.Execute uses processor cores. Therefore, if your processor has more than one core, they will be used equally to start this thread. Here you are m

0
source

Source: https://habr.com/ru/post/1339610/


All Articles