I've made the code below work and it is mostly non-blocking except where the process.start code is.
However, my question is, in my winforms application is this the best way to implement the use of async/await to prevent blocking?
Having googled a fair bit to get this far, I'm not terribly clear on what better way I could have done this code in this context, however I'd be grateful for any pointers.
At the UI Layer:
Private Async Sub getcontacts()
Dim t = ImportMUSContactsAsync()
Await t
If t.IsCompleted Then
BuildLookups()
MainForm.HideWait()
End If
End Sub
At the Datalayer:
Private ImportFiles As New List(Of MUSFile) 'Elided full definition
Public Async Function ImportMUSContactsAsync() As Task(Of Boolean)
Await Task.Run(Sub() DeleteExistingCSV())
Await Task.Run(Sub() ConvertTPSToCSV())
Await Task.Run(Sub() ReadCSVFiles())
Return True
End Function
Called Sub routines
Private Sub ConvertTPSToCSV()
Dim CSVFolder = New DirectoryInfo(CSVPmanPath)
If Not CSVFolder.Exists Then CSVFolder.Create()
For Each f In ImportFiles
If File.Exists(f.TPSFilename) Then
Dim p = New ProcessStartInfo($"{PathToConverter}", $" csv {f.TPSFilename} {f.CSVFilename}")
Process.Start(p).WaitForExit()
End If
Next
End Sub
Private Sub ReadCSVFiles()
For Each f In ImportFiles
If File.Exists(f.CSVFilename) Then ReadSingleCSV(f)
Next
End Sub
Private Sub ReadSingleCSV(f As MUSFile)
Using textReader As New StreamReader(f.CSVFilename)
Using csv As New CsvReader(textReader)
CSVMapToContact(csv, f)
End Using
End Using
End Sub