Private Async Function AddToLogAsync(ByVal LogData$) As Task
Dim retryCount As Integer = 0
While retryCount < 5
Try
Using writer As StreamWriter = File.AppendText(PhysicalPathOfThisFolder & "/updMsg.log")
Await writer.WriteLineAsync(LogData)
End Using
' If no exception was thrown, break the loop
Exit While
Catch ex As Exception
If TypeOf ex Is IOException AndAlso ex.Message.Contains("being used by another process") Then
' File is locked, wait and retry
Await Task.Delay(100)
retryCount += 1
Else
' Log the exception
Await AddToLogAsync("Error: " & ex.Message & vbCrLf & ex.StackTrace)
' If this is not a file locking exception, break the loop
Exit While
End If
End Try
End While
End Function