如何使用VB.NET 2012将控制台应用程序的输出重定向到Windows表单上的TextBox控件



团队,

我有一个第三方应用程序,该应用程序实际上是电话消息服务器,并在所有连接的客户端和其他服务器之间交换消息。这款消息服务器可在运行几天甚至用于飞蛾。这完全是一个控制台应用程序,没有任何GUI。即使管理该服务器的内部操作,还有另一个工具再次是基于控制台的应用程序。我想准备一个GUI以启动,停止和重新启动该服务器,以在VB.NET 2012中。我设法了,

  1. 创建此服务器的过程实例
  2. 使用适当的参数启动服务器并保持运行。以下是我应用程序启动服务器的一些示例代码,

    private sub server_start_click(发件人作为对象,e as eventargs)处理server_start.click 昏暗参数,server_admin_path作为字符串 server_admin_path =" d: voice_app datamessage messageerver.exe" 参数=" -properties"&""&" d: voice_app config message.prop"

    Dim proc = New Process()
    proc.StartInfo.FileName = server_admin_path
    proc.StartInfo.Arguments = parameter
    ' set up output redirection
    proc.StartInfo.RedirectStandardOutput = True
    proc.StartInfo.RedirectStandardError = True
    proc.EnableRaisingEvents = True
    Application.DoEvents()
    proc.StartInfo.CreateNoWindow = False
    proc.StartInfo.UseShellExecute = False
    ' see below for output handler
    AddHandler proc.ErrorDataReceived, AddressOf proc_OutputDataReceived
    AddHandler proc.OutputDataReceived, AddressOf proc_OutputDataReceived
    proc.Start()
    proc.BeginErrorReadLine()
    proc.BeginOutputReadLine()
    'proc.WaitForExit()
    Server_Logs.Focus()
    

    结束子

此代码很好地启动了消息服务器。消息服务器现在已经启动,并且在特定时间间隔表示30秒之后,它将在控制台上产生日志轨迹,并且将继续使用,直到管理工具不会停止消息服务器为止。因此,现在我想要的是捕获服务器在其控制台上生产的每一行,并将该行粘贴到我在Windows表单上的文本框上。

我得到的代码低于代码,这使我在生产时的每一行,

   Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As                     DataReceivedEventArgs)
    On Error Resume Next
    ' output will be in string e.Data
    ' modify TextBox.Text here
    'Server_Logs.Text = e.Data  ` Does not display anything in textbox
    MsgBox(e.Data) 'It works but I want output in text box field
End Sub

p.s =我的应用程序将处理更多这样的服务器和我不希望用户将每个消息服务器实例在其任务栏上打开,作为控制台窗口,并且他们滚动了长的日志轨迹。我在这里搜索了很多线程,但是在上面的情况下对我没有任何帮助。从很长一段时间以来,我就一直被困在这方面,这将不胜感激,现在这是一个展示!!!

看起来您正在尝试从与形式所打开的线程不同的线程中调用一个呼叫。从过程类提出的事件不会来自同一线程。

Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)
Public Sub UpdateTextBox(text As String)
    Textbox.Text = text
End Sub
Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    If Me.InvokeRequired = True Then
        Me.Invoke(myDelegate, e.Data)
    Else
        UpdateTextBox(e.Data)
    End If
End Sub

最新更新