Hi,
I have an app that displays specific folder file content in Window Forms and all this works fine. Problem is when I need to scan folder content again (it is read every 10 seconds), then I have "Not Responding" message if there is a lot of files to read. What would be the best way to scan files, as a Thread or BackgroundWorker (which I am using at the moment)? I don't want to have my app stucked at "not responing" message screen. Please help. Thanks!

One solution would be to use FileSystemWatcher class which would raise an event when the folder content has changed. After that you would scan the files.

BackgroundWorker should be fine for scanning the folder. I didn't test this idea but you could raise ProgressChanged event from your BackgroundWorker thread and handle that event in your UI thread (i.e. main form or similar). Event handler would just call Application.DoEvents() to keep UI thread's "message pump" processing and that would avoid "Not Responding" message.

If this didn't help you should post your code. Especially how you trigger folder scanning every tenth second, the code that actually does folder scanning and all the other code related to BackgroundWorker.

HTH

If you scan the folder when the app first starts and build a list of the files you can have your app notified when any new files are created. Here is an example (VB 2010) you can run. Just create a new project and add a listbox control named lbxFiles.

Public Class Form1

    Private WatchFolder As String = "D:\temp\watch"
    Private WithEvents fswFiles As New IO.FileSystemWatcher()

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        With fswFiles
            .Path = WatchFolder
            .SynchronizingObject = Me
            .Filter = "*.txt"
            .IncludeSubdirectories = False
            .NotifyFilter = IO.NotifyFilters.FileName
            .EnableRaisingEvents = True
        End With

    End Sub

    Private Sub fswFiles_Created(sender As System.Object, e As System.IO.FileSystemEventArgs) Handles fswFiles.Created

        lbxFiles.Items.Add(e.Name)

    End Sub

End Class

Set your filter to whatever types of files that you may want to restrict your notifications to. Use *.* to be notified of all new files. The gotcha is the line

.SynchronizingObject = Me

This tells the watcher object to use the foreground thread. If you don't do this then all notifications are done to a background thread and any attempts to update a foreground thread object will crash the app. You can comment this line out to see what happens. You can replace the line

lbxFiles.Items.Add(e.Name)

with whatever code you need to process the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.