实时控制台重定向问题

时间:2013-10-23 17:21:01

标签: vb.net real-time console-redirect

首先,我确实花了很多时间试图找到我的问题的答案,但到目前为止,没有。

这是问题所在。 我必须使用参数运行cli命令并将输出(REALTIME)重定向到文本框。 该命令将如下所示: smcli - n name -f“script.scr”

由于smcli不在系统PATH变量中,我首先需要转到右侧Dir

所以,我最终运行如下所示的完整命令: cmd / c cd“c:\ Program Files(x86)\ bla \”&& smcli - n name -f“script.scr”

我会把代码放在一秒钟但基本上,事情工作正常,我到达正确的目录,运行命令,它运行脚本,一切都很好。

我尝试将输出重定向到另一种形式但是它根本不再起作用;无论如何,这将是奖金问题。

但事情是,当脚本运行时,它会逐步说明它正在做什么并将该信息发送到输出(即恢复名称,恢复磁盘配置,......)

正如我所说,我确实得到了所有这些产出......一旦完成一切(即明显冻结5分钟后)。 不将输出重定向到文本框(即让控制台窗口保持打开状态),我实时获得输出。

现在这里是代码(有一点,对不起):

Private Sub ReadMDcfgToolStripButton_Click(sender As Object, e As EventArgs) Handles ReadMDcfgToolStripButton.Click
        Dim exitcode As Int32
        smcli = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'SMcli.exe"
        Dim gotosmclidir As String = "cd """ & smcli & """"
        Dim smclicommand As String
            smclicommand = "smcli -n " & mdname & " -c ""save storagearray configuration file=\""" & saveFileDialog2.FileName & "\"" allconfig"";"
            cmd = gotosmclidir & " && " & smclicommand
            exitcode = RunCommandCom(cmd, "", False)
End Sub

Private Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Int32
    ' Usage:
    'RunCommandCom("DIR", "/W", true)
    'For the multiple command on one line the key are the & | && and || command connectors
    '•A & B -> execute command A, then execute command B. 
    '•A | B -> execute command A, and redirect all it's output into the input of command B.
    '•A && B -> execute command A, evaluate the errorlevel after running Command A, and if the exit code (errorlevel) is 0, only then execute command B.
    '•A || B -> execute Command A, evalutate the exit code of this command and if it's anything but 0, only then execute command B.

    TextBox1.Text = "Communication in progress..." & vbCrLf
    exitcodelbl.Text = ""
    CmdCloseBtn.Enabled = False
    ToolStrip1.Enabled = False
    CmdOutputPanel.Visible = True

    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()


    pi.FileName = "cmd.exe"
    pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
    MsgBox(pi.Arguments)
    pi.CreateNoWindow = True
    pi.UseShellExecute = False
    pi.RedirectStandardOutput = True
    pi.RedirectStandardError = True
    pi.CreateNoWindow = True
    p.StartInfo = pi
    AddHandler p.OutputDataReceived, AddressOf GotData
    p.Start()
    p.BeginOutputReadLine()

    Do Until p.HasExited
    Loop

    exitcodelbl.Text = p.ExitCode
    Select Case p.ExitCode
        Case 0
            exitcodelbl.Text += " - Command completed successfully."
        Case 1
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case 9
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case 14
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case Else
            exitcodelbl.Text += " - Unknown code."
    End Select
    Return p.ExitCode
End Function

Sub GotData(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    UpdateTextBox(e.Data)
End Sub

Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)

Private Sub UpdateTextBox(ByVal Tex As String)
    If Me.InvokeRequired Then
        Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
        Dim args As Object() = {Tex}
        Me.Invoke(del, args)
    Else
        TextBox1.Text &= Tex & Environment.NewLine
    End If
End Sub

除了命令本身之外,其他所有内容都来自于研究,它们只是放在一起,一方面是运行命令,另一方面是重定向。

我认为这个问题与我的命令有关(看起来那部分非常明智)。

要么我能以不同的方式完成它(再次,它有效但不是实时)或者我错过了一些微不足道的东西......?

感谢您的帮助。

以下是tinstaafl,更新后的代码为:

Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    copyrightlbl.Focus()
    wait(1000)
    Await RunCommandCom("", "192.168.219.152", False) '172.16.1.55
    CmdCloseBtn.Enabled = True
End Sub

Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)
    TextBox1.Text = "Communication in progress..." & vbCrLf
    exitcodelbl.Text = ""
    CmdCloseBtn.Enabled = False
    ToolStrip1.Enabled = False
    CmdOutputPanel.Visible = True

    Dim p As Process = New Process()
    With p.StartInfo
        .WorkingDirectory = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'Directory for SMcli.exe"
        .FileName = "ping.exe"
        .Arguments = command + " " + arguments
        .CreateNoWindow = True
        .UseShellExecute = False
        .RedirectStandardOutput = True
        .RedirectStandardError = True
    End With

    p.Start()

    Do Until p.HasExited
        TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
    Loop
    exitcodelbl.Text = p.ExitCode
    Return p.ExitCode
End Function

1 个答案:

答案 0 :(得分:2)

尝试直接将输出读取到文本框,而不是使用委托。 Process的StandardOutput属性是一个流,您可以使用ReadLineAsync方法获取输出。这样的事情应该有效:

pi.CreateNoWindow = True
pi.UseShellExecute = False
pi.RedirectStandardOutput = True
pi.RedirectStandardError = True
p.StartInfo = pi
p.Start()

Do Until p.HasExited
   TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
Loop

Intellisense会提示你制作子程序Async,但是你应该得到你想要的输出。

Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)


Await RunCommandCom("", "192.168.219.152", False)