PawanS
PawanS

Reputation: 7193

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

Upvotes: 4

Views: 1415

Answers (4)

Andras Zoltan
Andras Zoltan

Reputation: 42363

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.

Upvotes: 1

Thanigainathan
Thanigainathan

Reputation: 1547

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

Upvotes: 0

TomTom
TomTom

Reputation: 62157

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.

Upvotes: 3

Jonathan Allen
Jonathan Allen

Reputation: 70337

You can print the Thread.ManagedThreadId value to see which threads are being used.

Upvotes: 0

Related Questions