0

团队,

我有一个第三方应用程序,它实际上是一个电话消息服务器,并在所有连接的客户端和其他服务器之间交换消息。该消息传递服务器会持续运行数天,甚至会持续运行数天。这完全是一个控制台应用程序,没有任何 GUI。甚至为了管理该服务器的内部操作,还有另一个工具是基于控制台的应用程序。我想准备一个 GUI 来在 VB.Net 2012 中启动、停止和重新启动此服务器。我已经设法,

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

    Private Sub Server_Start_Click(sender As Object, e As EventArgs) 处理 Server_Start.Click Dim 参数,server_admin_path As String server_admin_path = "D:\Voice_App\DataMessage\MessageServer.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

PS = 我的应用程序将处理多个这样的服务器,我不希望用户将每个消息服务器实例都作为控制台窗口在其任务栏上打开,并且他们正在滚动长日志跟踪。我在这里搜索了很多线程,但在上述情况下对我没有任何帮助。任何帮助都将不胜感激,因为我很长一段时间以来一直坚持这一点,现在这已经是一个很好的表现了!!!!

4

1 回答 1

3

看起来您正在尝试从与表单所在的线程不同的线程进行调用。从 Process 类引发的事件不会来自同一个线程。

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
于 2012-12-18T15:33:50.290 回答