Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Recently I discovered Parallel ForEach because I was still using VB 2010. In my application I open a hundred files to retrieve information and it takes a lot of time. I would like to succeed in using Parallel ForEach but I can't do it at all, I've been looking everywhere on google for 3 days ...
Could someone help me convert my little piece of code?

VB
Lv1.Items.Clear()
        Dim DirInv As DirectoryInfo
        Dim CheminFichierIni As String 'Path of my file of configuration
        If Debugger.IsAttached Then
            DirInv = New DirectoryInfo("\\w11900100akf\Mnt_CPAM\Inventaire\Log")
        Else
            DirInv = New DirectoryInfo(Path.Combine(Application.StartupPath, "Log"))
        End If
        ' Get a reference to each file in that directory.
        Dim FilInv As FileInfo() = DirInv.GetFiles()
        ' Display the names of the files.

        For Each FileInfoInv As FileInfo In FilInv 'Here i want to use Parallel For Each
            CheminFichierIni = FileInfoInv.FullName 'For each file, i read information
            Dim LstVItem As New ListViewItem(LireINI("PMF", "NetBios", CheminFichierIni))
            LstVItem.SubItems.Add(LireINI("PMF", "Owner", CheminFichierIni))
            LstVItem.SubItems.Add(... ...


Thank you

What I have tried:

a lot of code found on the internet but it doesn't lead to what I'm trying to do ...
Posted
Updated 25-Mar-21 0:36am
Comments
CHill60 23-Mar-21 11:40am
   
So what happens when you replace that For Each with Parallel.ForEach?

As Chill60 has said: try replacing For Each with Parallel.ForEach:
VB
Parallel.ForEach(FileInv, Sub(FileInfoInv)
                          CheminFichierIni = FileInfoInv.FullName
                          ...    
                          End Sub)


But ... threading and parallelism isn't a "magic bullet" that will make your code 100 times faster: each object in the collection requires it's own thread, and to run that thread needs a free processor core. If no core is available, the thread goes into a wait state waiting for one to be free. So the number of cores in your system and the load on the system outside your app will decide how much faster you app will be. It is entirely possible that creating too many threads will cause your whole app to slow down rather than speed up as all the threads are "fighting" to get a core and the threading process plus switching threads adds overhead to the processor. I would not recommend 100 threads at all - and there is a very good chance that your app will crash as well given that ListView is not thread safe, and you are accessing UI controls from a non-UI thread ...
   
Hello,

You're right, when reading a file it doesn't change much
Thank you for your help
   
Comments
Richard Deeming 25-Mar-21 8:56am
   
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as a new solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900