Ok, I figured it out. The GUI thread cannot update the controls as we would
think. You have to use a background thread:
I found a link to help you get there.
'
http://msdn.microsoft.com/library/?url=/library/en-us/dv_vstechart/html/vbasync.asp
I may have implemented it wrong, but it certainly works.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Value = 0
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Show()
Dim t As System.Threading.Thread
t = New System.Threading.Thread(AddressOf Process)
t.Start()
While t.IsAlive
Application.DoEvents()
End While
Close()
End Sub
Public Sub Process()
Dim iProgress As Integer
Const searchpath As String = "c:\test"
Const archivepath As String = "c:\archived"
Try
Dim filelist As ArrayList
Dim sFilePath As String
Dim sArchiveFilePath As String
filelist = New ArrayList
filelist.AddRange(System.IO.Directory.GetFiles(searchpath, "*.*"))
For Each sFile As String In filelist
ClientUpdateProgress(sFile, filelist.IndexOf(sFile), filelist.Count)
System.Threading.Thread.CurrentThread.Sleep(1000)
sFilePath = System.IO.Path.Combine(searchpath, sFile)
sArchiveFilePath = System.IO.Path.Combine(archivepath,
System.Guid.NewGuid.ToString("N"))
If (System.IO.File.GetAttributes(sFilePath) And Not
System.IO.FileAttributes.Archive) = IO.FileAttributes.Archive Then
Try
System.IO.File.Copy(sFilePath, sArchiveFilePath)
WriteToCatalog(sFilePath, sArchiveFilePath)
System.IO.File.SetAttributes(sFilePath,
System.IO.File.GetAttributes(sFilePath) Or IO.FileAttributes.Archive)
Catch ex As Exception
Trace.WriteLine(String.Format("Failed to archive {0}. The
exception was {1}", sFile, ex))
End Try
End If
SetFileProgress(sFile)
System.Threading.Thread.CurrentThread.Sleep(1000)
Next
Catch ex As Exception
Trace.WriteLine(String.Format("An unexpected exception occured during
the archive process. The exception was {0}", ex))
End Try
End Sub
Public Sub ClientUpdateProgress(ByVal FileName As String, ByVal
CurrentStep As Integer, ByVal TotalSteps As Integer)
Trace.WriteLine(String.Format("{0}, {1}, {2}", FileName, CurrentStep,
TotalSteps))
Dim iTemp As Integer
Me.Label1.Text = FileName
Me.Label2.Text = "Processing"
iTemp = Int(CurrentStep / TotalSteps * 100)
Me.ProgressBar1.Value = iTemp
End Sub
Public Sub SetFileProgress(ByVal FileName As String)
Me.Label1.Text = FileName
Me.Label2.Text = "Completed"
End Sub
<MTAThread()> Public Shared Sub Main()
Dim fProgress As Form1
fProgress = New Form1
Application.Run(fProgress)
End Sub
End Class