-1

我使用 Process.start() 运行多个具有不同名称的 exe,并将 Raisingevents 启用为 True。为了检查进程的状态,我在进程退出事件中提示用户并向用户显示消息。

但问题是我想在该退出事件中向用户显示特定的 Exe 名称。我的 For process Start 代码是:

Private Sub StartExe()
Private psi As ProcessStartInfo
Private cmd As Process
Dim filePath As String = "vision.exe"
psi = New ProcessStartInfo(filePath)
Dim systemencoding As System.Text.Encoding = _
        System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
    With psi
        .Arguments = "Some Input String"
        .UseShellExecute = False   
        .RedirectStandardError = True
        .RedirectStandardOutput = True
        .RedirectStandardInput = True
        .CreateNoWindow = False
        .StandardOutputEncoding = systemencoding  
        .StandardErrorEncoding = systemencoding
    End With
    cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
    AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
    AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
    AddHandler cmd.Exited, AddressOf processExited
    cmd.Start()
    cmd.BeginOutputReadLine()
    cmd.BeginErrorReadLine()
  End Sub
  'For Receiving the Output of Exe, I used a TextBox to view
   Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As   DataReceivedEventArgs)
  Me.Invoke(New InvokeWithString(AddressOf Sync_Output1), e.Data)
  End Sub
   Private Sub Sync_Output1(ByVal text As String)
    txtLog.AppendText(text & Environment.NewLine)
  End Sub
 'At the Exit event, I inform the user that an Exe exited due to some reason etc.
  Private Sub processExited(ByVal sender As Object, ByVal e As EventArgs)
     Me.BeginInvoke(New Action(Function()
     MessageBox.Show("The Stream for " &Particular Exe&   "is Exited.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

     End Function))
     End Sub

在 Process Exited Event 中,如何显示触发该事件的特定 Exe 的名称。就像在那个特定的代码中一样,我启动了一个“vision.exe”,所以我想通知用户 vision.exe 由于某种原因等被终止。

4

1 回答 1

1

到 Exited 事件运行时,该进程已经死亡,您无法再检索它的属性。由于您已经使用了 lambda 表达式,您也可以通过编写一个捕获filePath变量的表达式来解决这个问题。像这样:

    AddHandler cmd.Exited,
        Sub(s, e)
            Me.BeginInvoke(New Action(
                Sub()
                    MessageBox.Show(filePath + " has ended")
                End Sub))
        End Sub

请注意,在进程终止或您自己的程序退出之前,您必须保持 Form 对象处于活动状态。如果您不这样做,那么 BeginInvoke() 调用将在已处置的表单上进行,这也是您原始代码中的一个问题。您可以通过检查 Me.InvokeRequired 来避免这种情况。如果它返回 false 那么什么也不做。

于 2013-09-15T20:49:26.983 回答