I have made an application in vb.net which launches a cmd like process in the background, reads its output asynchronously and redirects it in a textbox. Normally my application should show every new output line in the textbox in real time. Unfortunately here is what happens:
These are the first output lines which are shown in the textbox.
After a few seconds an unwanted pause occurs for some reason. No new lines are shown in the textbox even though there is output from the process. After a few minutes all that output i couldn't see because of the pause, shows up and then a pause occurs again.
Finally, the last bunch of lines show up and the process is terminated.
Here is my code:
Private Sub StartSteamCMD()
Try
Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe", "/C steamcmd +runscript " + temp + "Setti_SRCDS_Manager\install_update.txt")
With psi
.UseShellExecute = False
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.CreateNoWindow = True
.StandardOutputEncoding = Encoding.GetEncoding(CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
.StandardErrorEncoding = Encoding.GetEncoding(CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
End With
process = New Process With {.StartInfo = psi}
AddHandler process.OutputDataReceived, AddressOf Async_Data_Received
AddHandler process.ErrorDataReceived, AddressOf Async_Data_Received
process.Start()
process.BeginOutputReadLine()
process.BeginErrorReadLine()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Async_Data_Received(sender As Object, e As DataReceivedEventArgs)
Try
Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Sync_Output(text As String)
Try
TextBox1.AppendText(text + Environment.NewLine)
TextBox1.ScrollToCaret()
If Not String.IsNullOrEmpty(text) Then
If text.Contains("downloading") Or text.Contains("validating") Then
<code to animate the progress bar here>
ElseIf text.Contains("Success") Then
<the server is installed successfully, some code to run here>
ElseIf text.IndexOf("error", 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
<some code to run in case of error>
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Any thoughts of what can cause those pauses? I have been thinking the following. As you can see in the first screenshot, the last two lines are about establishing a connection with Steam servers (the place where the game server files will be downloaded from) and waiting for user info. Maybe that moment the process doesn't produce any output for a few seconds. So at some point during those seconds, the first pause of async reading occurs. Then the process starts producing output again but it's not redirected to the textbox because of the pause. Could it be the async reading gets paused because of the process not producing any output for a few seconds?
I don't know what else to think. I'm tired of searching the internet for a solution, because i can't find any post about my problem or at least something similar. I hope you guys can help me out. Thanks in advance