Having a bit of an issue with my file.scanner and that issue is that I cannot figure out how to run multi.threading when searching the files within a folder(8,000+ files).

Imports System.IO
Module x
    Function xGetFolderFilesCount(ByVal selFolder As String) As Integer
        If Directory.Exists(selFolder) Then Return Directory.GetFiles(selFolder).Length Else Return 0
    End Function
    Function xLoadFolderFiles(ByVal selFolder As String, Optional ByVal searchPattern As String = "*", _
                              Optional ByVal searchOption As SearchOption = SearchOption.TopDirectoryOnly) As Array
        If Directory.Exists(selFolder) Then Return Directory.GetFiles(selFolder, searchPattern, searchOption) Else Return Nothing
    End Function
End Module

Public Class Form1
    Public myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\TEMP.FILES\"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not xGetFolderFilesCount(myFolder) = 0 Then
            For Each itm As String In xLoadFolderFiles(myFolder)
                '// Run multi.threading here.
        End If
    End Sub
End Class

I tried System.Threading.Thread and using the following...

Private Sub scanFile()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf scanFile))
            '// code here to scan file.
        End If
    End Sub

...which returned the search.results at the same rate as it did without using the System.Threading.Thread .

Any and all input on this issue is welcomed.
Thanks in advance,

5 Years
Discussion Span
Last Post by codeorder

I managed to find a solution with the help of a vb.net friend from Brazil.

Private t1, t2 As Thread, t1S, t1E, t2S, t2E As Integer
    Sub searchFiles()
        lv.Items.Clear() : With prg : .Value = 0 : .Maximum = arFiles.Length : End With
        t1 = New Thread(AddressOf tr1) : t1S = 0 : t1E = 4000 : t1.Start()
        t2 = New Thread(AddressOf tr2) : t2S = 4001 : t2E = arFiles.Length - 1: t2.Start()
    End Sub
    Sub tr1()
        For i As Integer = t1S To t1E : scanFile(arFiles(i)) : If isSearchActive = False Then : Exit Sub : End If : Next
    End Sub
    Sub tr2()
        For i As Integer = t2S To t2E : scanFile(arFiles(i)) : If isSearchActive = False Then : Exit Sub : End If : Next
    End Sub
    Private Sub scanFile(ByVal selFile As String)
        '// scan file code here.
    End Sub

Above code is for only 2 threads, though I declared 9 threads(System.Threading.Thread) for my app, assigned each thread it's own start and end index(separate Integers) by splitting the entire folder file.count into as many threads as I needed for it to run and it blew me out of my chair when I pressed the start button.

Prior to using Threading, it would take around 30+/- seconds to scan my files, and after adding the threading to my.app, it scanned in 5+/- seconds, if not less.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.