While foldersToProcess.Count > 0
....
Catch ex as Exception
worker.ReportProgress(0, "Cannot access folder: " & erFile)
End Try
....
worker.ReportProgress(10, New List(Of Integer) From {NumberOfFiles, NumberOfErrors}
)
End While
The simply change the progress handler to filter on the progress percentage, 0 for updating textbox3 and 10 for updating textboxes 4 and 5 (also bear in mind the different types being returned by each percentage, i.e string and list of integer respectively)
Public Class Form1
Private Sub btnSetStartFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnSetStartFolder.Click
Using fbd As New FolderBrowserDialog
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = fbd.SelectedPath
End If
End Using
End Sub
Private Sub btnSetOutpuFile_Click(sender As System.Object, e As System.EventArgs) Handles btnSetOutpuFile.Click
Using sfd As New SaveFileDialog
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox2.Text = sfd.FileName
End If
End Using
End Sub
Private Sub btnStartFindFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnStartFindFiles.Click
btnStartFindFiles.Enabled = False
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
Dim FF As New FindFiles(TextBox1.Text, TextBox2.Text)
AddHandler FF.FindFilesError, AddressOf FF_FindFilesError
AddHandler FF.FindFilesProgress, AddressOf FF_FindFilesProgress
AddHandler FF.FindFilesFailed, AddressOf FF_FindFilesFailed
AddHandler FF.FindFilesComplete, AddressOf FF_FindFilesComplete
FF.StartAnalyze()
End Sub
Private Sub FF_FindFilesError(FileName As String)
TextBox3.AppendText(FileName & vbCrLf)
End Sub
Private Sub FF_FindFilesProgress(NumberOfFiles As Integer, NumberOfErrors As Integer)
TextBox4.Text = NumberOfFiles
TextBox5.Text = NumberOfErrors
End Sub
Private Sub FF_FindFilesFailed(Failure As String)
MessageBox.Show(Failure, "FindFiles() Failed!")
End Sub
Private Sub FF_FindFilesComplete()
MessageBox.Show("Done!", "FindFiles() Complete!")
btnStartFindFiles.Enabled = True
End Sub
End Class
Public Class FindFiles
Private _OutputPath As String = ""
Private _SelectedFolderPath As String = ""
Private T As System.Threading.Thread = Nothing
Private SC As System.Threading.SynchronizationContext = Nothing
Public Event FindFilesError(ByVal FileName As String)
Public Event FindFilesProgress(ByVal NumberOfFiles As Integer, ByVal NumberOfErrors As Integer)
Public Event FindFilesFailed(ByVal Failure As String)
Public Event FindFilesComplete()
Private Sub New()
End Sub
Public Sub New(ByVal SelectedFolderPath As String, ByVal OutputPath As String)
_OutputPath = OutputPath
_SelectedFolderPath = SelectedFolderPath
SC = System.Windows.Forms.WindowsFormsSynchronizationContext.Current
End Sub
Public Sub StartAnalyze()
If IsNothing(T) Then
T = New System.Threading.Thread(AddressOf BackgroundProcess)
T.Start()
End If
End Sub
Private Sub BackgroundProcess()
Try
Using sw As New System.IO.StreamWriter(_OutputPath)
FindFiles(_SelectedFolderPath, sw)
End Using
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseFailed), ex.Message)
End If
End Try
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseComplete), Nothing)
End If
End Sub
Private Sub FindFiles(ByVal folder As String, ByVal sw As System.IO.StreamWriter)
Dim NumberOfFiles As Integer = 0
Dim NumberOfErrors As Integer = 0
Dim dt As DateTime = DateTime.Today
Dim di As System.IO.DirectoryInfo
Dim fi As System.IO.FileInfo
Dim foldersToProcess As New List(Of System.IO.DirectoryInfo)
Try
foldersToProcess.Add(New System.IO.DirectoryInfo(folder))
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseFailed), ex.Message)
End If
Exit Sub
End Try
While foldersToProcess.Count > 0
di = foldersToProcess(0)
foldersToProcess.RemoveAt(0)
Try
For Each fi In di.GetFiles
NumberOfFiles = NumberOfFiles + 1
sw.WriteLine(fi.FullName)
Next
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseError), di.FullName)
End If
NumberOfErrors = NumberOfErrors + 1
End Try
Try
foldersToProcess.AddRange(di.GetDirectories)
Catch ex As Exception
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseError), di.FullName)
End If
End Try
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseProgress), New Integer() {NumberOfFiles, NumberOfErrors})
End If
End While
End Sub
Private Sub RaiseError(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesError(state)
End Sub
Private Sub RaiseProgress(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
Dim values() As Integer = CType(state, Integer())
RaiseEvent FindFilesProgress(values(0), values(1))
End Sub
Private Sub RaiseFailed(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesFailed(state.ToString)
End Sub
Private Sub RaiseComplete(ByVal state As Object)
' *** DO NOT CALL DIRECTLY! ***
' Already Post()ed with the SynchronizationContext:
RaiseEvent FindFilesComplete()
End Sub
End Class
Open in new window