2017-04-21 54 views
0

我正在尝试编写一个简单的vb程序来将标准输入/输出同步到一个文本框。程序首先应该找到一个exe文件,然后运行该文件并输出到文本框,然后关闭该文件,然后重新运行该文件或运行其他文件。第一次事情会很好,但是当我关闭文件并尝试重新运行时,我无法再获得输出。请让我知道什么是错的。下面的代码:打开一个进程,获取输出,然后打开另一个进程并获得输出

Public Class Form1 

Dim P As New Process 
Dim SW As System.IO.StreamWriter 
Dim fd As New OpenFileDialog 
Dim progName As String 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    P.StartInfo.CreateNoWindow = False 
    P.StartInfo.UseShellExecute = False 
    P.StartInfo.RedirectStandardInput = True 
    P.StartInfo.RedirectStandardOutput = True 
    P.StartInfo.RedirectStandardError = True 
    P.SynchronizingObject = TextBox1 

    fd.Title = "Open Program" 
    fd.InitialDirectory = "C:\windows\system32" 
    fd.Filter = "EXE program | *.exe" 
    fd.FilterIndex = 1 
    fd.RestoreDirectory = True 

End Sub 

Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs) 
    TextBox1.AppendText(output.Data() & vbCrLf) 
End Sub 

Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 

    Static Line As String 

    If e.KeyChar = Chr(Keys.Return) Then 

     SW.WriteLine(Line & vbCrLf) 

     Line = "" 

    Else 

     Line = Line & e.KeyChar 

    End If 

End Sub 

Private Sub OpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFile.Click 
    If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then 
     progName = fd.FileName 
     TextBox1.AppendText(progName & vbCrLf) 
    End If 
End Sub 

Private Sub ButtonRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRun.Click 

    ButtonRun.Enabled = False 
    ButtonStop.Enabled = True 

    AddHandler P.OutputDataReceived, AddressOf DisplayOutput 
    AddHandler P.ErrorDataReceived, AddressOf DisplayOutput 
    P.StartInfo.FileName = progName 
    P.Start() 

    P.BeginOutputReadLine() 

    SW = P.StandardInput 
    SW.WriteLine() 

End Sub 

Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click 
    ButtonStop.Enabled = False 
    ButtonRun.Enabled = True 
    P.CancelOutputRead() 
    RemoveHandler P.OutputDataReceived, AddressOf DisplayOutput 
    RemoveHandler P.ErrorDataReceived, AddressOf DisplayOutput 
    ''P.CloseMainWindow() 
    P.Close() 
End Sub 

末级

回答

0

您需要重新创建您想自Process objects cannot be reused运行一个程序,每次你的流程实例。

所以,定义P

Dim P As Process ' Notice no `new` here 

和移动它的初始化代码Form1_LoadButtonRun_Click

Private Sub ButtonRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRun.Click 

    ButtonRun.Enabled = False 
    ButtonStop.Enabled = True 

    ' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 
    P = new Process 
    P.StartInfo.CreateNoWindow = False 
    P.StartInfo.UseShellExecute = False 
    P.StartInfo.RedirectStandardInput = True 
    P.StartInfo.RedirectStandardOutput = True 
    P.StartInfo.RedirectStandardError = True 
    P.SynchronizingObject = TextBox1 
    ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

    AddHandler P.OutputDataReceived, AddressOf DisplayOutput 
    AddHandler P.ErrorDataReceived, AddressOf DisplayOutput 
    P.StartInfo.FileName = progName 
    P.Start() 

    P.BeginOutputReadLine() 

    SW = P.StandardInput 
     SW.WriteLine() 

End Sub 
+0

中提琴!现在完美!谢谢德米特里!周末愉快。 – Heibear

相关问题