In my code I will run a process and retrieve part of the standard error output (a number) to raise an event which send a progress number.
Sometimes I have an exception at this line at ".First" method 'cause the string has nothing to split:
out = mp3gain_For_NonTag.StandardError.ReadLine.Trim.Split("%").First
The problem Is I want to add the necessary check to avoid that exception without doing a Try/Catch because in my speed tests I've noticed that a try/catch in this procedure will slow the performance a lot.
I want to simplify the code doing the necessary checks but without a catching any exception (By the moment the checks I've added would be executed quickly than a try/catch).
Here is the code:
Private Shared Sub Run_MP3Gain_NotTag()
mp3gain_For_NonTag.Start() ' Run process
Dim out as string = mp3gain_For_NonTag.StandardError.ReadToEnd
While Not mp3gain_For_NonTag.HasExited
If Not String.IsNullOrEmpty(out) Then
' This would generate numbers between 1 to 100
out = mp3gain_For_NonTag.StandardError.ReadLine.Trim.Split("%").First
End If
If Integer.TryParse(out, 0) Then
RaiseEvent MP3Gain_Progress(out)
End If
End While
RaiseEvent MP3Gain_Exited()
End Sub
...And for additional information, this is a sample of what I don't want to do, I can say that adding a try catch like in this modificated code code even without catching any exception kind this will reduce the performance a lot:
Private Shared Sub Run_MP3Gain_NotTag()
mp3gain_For_NonTag.Start() ' Run process
Dim out as string = mp3gain_For_NonTag.StandardError.ReadToEnd
While Not mp3gain_For_NonTag.HasExited
Try
out = mp3gain_For_NonTag.StandardError.ReadLine.Trim.Split("%").First
RaiseEvent MP3Gain_Progress(out)
Catch : End Try
End While
RaiseEvent MP3Gain_Exited()
End Sub