Source thread: Async ProgressBar not working, answered by Peter Fleischer.
I'm trying to set up an async read of a bunch of files with a ProgressBar, but it doesn't work.
Here is my MainWindow:
Class MainWindow
Dim TheProgress As Progress(Of Integer)
Dim ProgCntr As Integer
Dim CToken As CancellationToken
And the process code:
Public Async Sub StartFntRead(TheFiles As List(Of IO.FileInfo), TheTxt As String)
With Me
.ProgBar.Value = 0
.ProgBar.Maximum = TheFiles.Count
.ProgBar.Visibility = Windows.Visibility.Visible
End With
With Me.LablProgText
.Content = TheTxt
.Visibility = Windows.Visibility.Visible
End With
ProgCntr = 0
CToken = New CancellationToken
TheProgress = New Progress(Of Integer)(AddressOf ReportProgress)
Await ReadFntFiles(CToken, TheFiles, TheProgress)
Me.ProgBar.Visibility = Windows.Visibility.Hidden
Me.LablProgText.Visibility = Windows.Visibility.Hidden
End Sub
Public Async Function ReadFntFiles(CTokn As CancellationToken, TheFiles As List(Of IO.FileInfo), Prog As IProgress(Of Integer)) As Task(Of Integer)
Dim Fnt As FntData
For Each AFile In TheFiles
ProgCntr += 1
Prog.Report(ProgCntr)
Fnt = New FontUtils.FntData With {.FName = AFile.FullName}
Select Case System.IO.Path.GetExtension(AFile.FullName).ToUpper
Case Is = ".TTF", ".OTF"
Call FontUtils.ReadTTOT(Fnt)
Case Is = ".PFM"
Call FontUtils.ReadT1(Fnt)
End Select
If Fnt.IsOK Then
TheAvailFnts.Add(Fnt)
Else
FntErrsSet.Fnts.Add(Fnt)
End If
Next AFile
End Function
Public Sub ReportProgress(ByVal ProgNum As Integer)
Me.ProgBar.Value = ProgNum
End Sub
To start processing files, call the StartFntRead method:
Call StartFntRead(TheFiles, "Reading font files")
Any suggestion?