2,854 questions
Hi,
Welcome to our Microsoft Q&A platform!
You can try the following demo:
Imports System.IO
Imports System.Threading
Public Class Window1
Private ProgCntr As Integer
Private CToken As CancellationTokenSource
Private TheProgress As New Progress(Of Integer)
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim listFI As List(Of FileInfo)
listFI = (From item In My.Computer.FileSystem.GetFiles("c:\temp") Select New FileInfo(item)).ToList
StartFntRead(listFI, "txt")
End Sub
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
CToken = New CancellationTokenSource()
Dim ct As CancellationToken = CToken.Token
AddHandler TheProgress.ProgressChanged, Sub(s, n)
ProgBar.Value = n
End Sub
Await Task.Run(Sub() ReadFntFiles(TheFiles, TheProgress, CToken.Token), CToken.Token)
MsgBox("Back after ReadFntFiles")
Me.ProgBar.Visibility = Windows.Visibility.Hidden
Me.LablProgText.Visibility = Windows.Visibility.Hidden
End Sub
Public Sub ReadFntFiles(TheFiles As List(Of IO.FileInfo), ByVal progress As IProgress(Of Integer), ByVal ct As CancellationToken)
' comment out for demo purposes
'Dim Fnt As FntData
'' with following method you can create a async thread and return awaitable object
'Dim ProgCntr As Integer = 0
'For Each AFile In TheFiles
' If ct.IsCancellationRequested Then Exit For
' ProgCntr += 1
' progress.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
' 'MsgBox(Fnt.DisplayName)
'Next AFile
' simalation of work
For i = 1 To 100
If ct.IsCancellationRequested Then Exit For
Thread.Sleep(100) ' simulate working time
progress.Report(i)
Next
End Sub
End Class
Thanks.