The proc.start() hang and unable to close

Eekhay 61 Reputation points
2023-09-07T00:27:55.21+00:00

The code below is unable to exit and hang the application. After pxx_gen.exe is executed, it will request the user to input 8-digit number then it complete. I'm trying to fix it but still no help, don't know what went wrong.

Dim SerNum_Path As String = "C:\Honey\plx_gen.exe"

Private Sub btnProgram_SerNum_Click(sender As Object, e As EventArgs) Handles btnProgram_SerNum.Click
        Dim reader As StreamReader
        Dim outline As String
        Dim failflag As String = Nothing

        lblProgramming_Status.Text = String.Empty
        lblProgramming_Status.Text = "Generate..."
        
        reader = Program_the_Number("cmd.exe", "plx_gen.exe", SerNum_Path, tbxNumber.Text.Trim)

        While Not reader.EndOfStream
            outline = reader.ReadLine()
            rtbProgramming_Console.AppendText(outline & vbCrLf)
            Console.WriteLine(outline)

            If outline.Length > 8 Then

                If outline.IndexOf("file open hb_b0_sn.bin") <> -1 Then
                    failflag = reader.ReadLine()
                    rtbProgramming_Console.AppendText(failflag & vbCrLf)
                End If

            End If
        End While

        If failflag <> ("file open hb_b0_sn_new.bin") Then

            lblProgramming_Status.Text = String.Empty
            lblProgramming_Status.Text = "Fail"
            Return

        End If

        lblProgramming_Status.Text = String.Empty
        lblProgramming_Status.Text = "Generate Done"

    End Sub

Public Function Program_the_Number(ByVal programName As String, ByVal cmdString As String, ByVal programPath As String, ByVal SNumber As String) As StreamReader         
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()                  proc.StartInfo.CreateNoWindow = True         
proc.StartInfo.FileName = programName         
proc.StartInfo.UseShellExecute = False         
proc.StartInfo.RedirectStandardError = True         
proc.StartInfo.RedirectStandardInput = True         
proc.StartInfo.RedirectStandardOutput = True         
proc.Start()      
    
If programPath.Length > 17 Then             
proc.StandardInput.WriteLine(programPath(0).ToString() & ":")              
If cmdString.Length <> 0 Then                 
proc.StandardInput.WriteLine("cd " & programPath.Substring(0, (programPath.Length - 17)))                    End If         
End If     
     
proc.StandardInput.WriteLine(cmdString)         
proc.StandardInput.WriteLine(SNumber)         
proc.StandardInput.WriteLine("exit")         
Dim reader As StreamReader = proc.StandardOutput         
proc.Close()         
Return reader      
End Function
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,322 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 19,936 Reputation points Microsoft Vendor
    2023-09-07T01:26:08.9666667+00:00

    Hi @Eekhay ,

    Try using Process.WaitForExit Method to make sure that the external process finishes execution before proceeding further in your code.

    Also please check Process.StandardError Property.

    Synchronous read operations introduce a dependency between the caller reading from the StandardError stream and the child process writing to that stream. These dependencies can result in deadlock conditions.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful