Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I found a piece of code that works very well (credits to the author, it's called "ThreadedFileCollector") for my purpose of listing specific files over a remote network. My additional requirement is a report to the UI, in order to signal that the "long lasting task" is still running and no error froze the screen.

Unfortunately I don't have a very good idea what at runtime really happens within the queries (I would guess it's LINQ?). Would you know where / how I would have to insert the code to update the UI?

Thanks a lot for any ideas or snippets.


What I have tried:

That's the function and sub:
Public Function CollectFiles(ByVal dir As String, ByVal pattern As String) As List(Of FileInfo)

        Dim di As New DirectoryInfo(dir)
        Dim queue As New ConcurrentQueue(Of FileInfo)()
        InternalCollectFiles(di, pattern, queue)
        Return queue.AsEnumerable().ToList()

    Catch ex As Exception
        MsgBox(String.Format("Error in 'CollectFiles': {0}", ex.Message))
        Return Nothing

    End Try

End Function

Private Sub InternalCollectFiles(ByVal dir As DirectoryInfo, ByVal pattern As String, ByVal queue As ConcurrentQueue(Of FileInfo))

    If dir.FullName.Contains(My.Computer.Name) Then
        ' new directoryinfo, if single computer
        dir = New DirectoryInfo(Right(dir.FullName, 2).Replace("$", ":\"))
    End If

        For Each result As FileInfo In dir.GetFiles(pattern).Select(Function(file) file) _
         .Where(Function(s) supportedExtensions.Contains(Path.GetExtension(s.Extension).ToLower()))
            'Dim msg As String = String.Format("{0}", dir.FullName)
            'UIDelegate.Invoke(msg, "")
        Next result
        Task.WaitAll(dir.GetDirectories().Select(Function(d) Task.Factory.StartNew(Sub() InternalCollectFiles(d, pattern, queue))).ToArray())

    Catch e As UnauthorizedAccessException
    Catch e As SecurityException
    Catch e As DirectoryNotFoundException

    Catch ex As Exception
        MsgBox(String.Format("Error in 'InternalCollectFiles': {0}", ex.InnerException))

    End Try

End Sub
Updated 4-Feb-19 22:58pm
Dave Kreskowiak 1-Feb-19 19:35pm    
Progress of what? You cannot know the number of files and directories involved without walking the directory tree ahead of time. So unless you do that, you have no way of determining what 100% means.

That means, the best you can do for "progress" is a marquee. A continuously spinning bar or icon that just says "something is happening", but there is no way to determine how long it's going to take.
Sonhospa 4-Feb-19 5:39am    
Hi Dave, thank you (again) for your answer. Sorry I used the wrong link (not 'reply') to answer so obvously didn't get notified - another member gave that hint to my error.
Sonhospa 1-Feb-19 19:51pm    
Thank you, Dave. I intended to (e.g.) show the currently searched directory in a statusbar. The call to a delegate is commented (green) as when active it resulted in an error (someth. like at breakmark "Code doesn't reach this line and has changed".

I am unsure if it would be wise to give permission to (wichever) changes.
CHill60 4-Feb-19 4:53am    
Use the Reply link on a comment to notify the member that you have responded
Sonhospa 4-Feb-19 5:41am    
Thank you, Chill60. I didn't get that right, so your hint relly helps.

1 solution

Would you know where / how I would have to insert the code to update the UI?

As we discussed here: Why 'part of path not found' error?[^], enumerating files and folders in a proper way is bit difficult, especially when you use a lot of collection types that are not thread-safe. The way you grab the data in process of enumerating files and folders may also affect on time and reaction UI (hangs/freezes).

So, you have 3 possibilities:
1) not display a progress bar,
2) display a progress bar:
a) computing a cost of operation on start (like Windows OS do while copying/deleting files/folders),
b) displaying information while performing the operation (in case when a total number or size or count of stuff is unknown)

Keep in mind that there's few types of progress bars, see: About Progress Bar Controls - Windows applications | Microsoft Docs[^]
- Range and Current Position - represents the progress that the application has made toward completing the operation,
- Marquee style - shows activity but does not indicate what proportion of the task is complete.

I'd choose Marquee style progress bar. In case you want to display progress (by using progress bar or by refreshing UI, for example ListBox), you'll be in need to use Control.Invoke Method (System.Windows.Forms) | Microsoft Docs[^]. See:
How to: Make Thread-Safe Calls to Windows Forms Controls | Microsoft Docs[^]
How to: Use a Background Thread to Search for Files | Microsoft Docs[^]

Good luck!
Share this answer

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