简体   繁体   English

如何在运行时使用vb.net 2012将控制台应用程序的输出重定向到Windows窗体上的Textbox控件

[英]How to redirect Output of console application to Textbox control on a windows form at run time using vb.net 2012

Team, 球队,

I have an third party application which actually is a telephonic messaging server and exchange messages between all connected clients and other servers. 我有一个第三方应用程序,它实际上是一个电话消息传递服务器,并在所有连接的客户端和其他服务器之间交换消息。 This messaging server keeps running for several days and even for moths. 该消息传递服务器可以连续运行几天甚至不停飞蛾。 This is entirely a console application and do not have any GUI. 这完全是一个控制台应用程序,没有任何GUI。 Even to manage the internal operations of this server, there is another tool which is a console based application again. 甚至为了管理该服务器的内部操作,还有另一个工具是基于控制台的应用程序。 I would like to prepare a GUI to start, stop and restart this server in VB.Net 2012. I have managed to, 我想准备一个GUI来启动,停止和重新启动VB.Net 2012中的服务器。

  1. Create the process instance of this server 创建此服务器的流程实例
  2. Launch the Server with appropriate parameters and keep it running. 使用适当的参数启动服务器并使其运行。 Below is some sample code from my application to launch the server, 以下是我的应用程序中用于启动服务器的一些示例代码,

    Private Sub Server_Start_Click(sender As Object, e As EventArgs) Handles Server_Start.Click Dim parameter, server_admin_path As String server_admin_path = "D:\\Voice_App\\DataMessage\\MessageServer.exe" parameter = " -properties " & """" & " D:\\Voice_App\\Config\\message.prop" 私有子Server_Start_Click(发送者作为对象,e作为EventArgs)处理Server_Start.Click昏暗参数,server_admin_path作为字符串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() 

    End sub 结束子

This code launches the message server very well. 此代码可以很好地启动消息服务器。 The message server is now started and it is producing log traces on the console after specific time of interval say 30 seconds and this will be continue till message server is not stopped by administration tool. 消息服务器现在已启动,并且在特定时间间隔(例如30秒)后,正在控制台上生成日志跟踪,此过程将继续进行,直到管理工具未停止消息服务器为止。 So now what I want is to capture every single line that is being produced by my server on its console and paste that line on to the Textbox I have on my windows form. 因此,现在我想要的是捕获服务器在其控制台上产生的每一行,并将该行粘贴到Windows窗体上的文本框中。

I got below code which gives me that every line as and when produced, 我得到了下面的代码,该代码使我知道每行以及何时生成的每一行,

   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 = My application will be handling more that one such servers and I don't want users to have every message server instance open on their taskbar as console window and they are scrolling long log traces. PS =我的应用程序将处理更多这样的服务器,我不希望用户在其任务栏上将每个消息服务器实例作为控制台窗口打开,并且他们正在滚动长的日志跟踪。 I searched lots of threads here but nothing worked for me in above scenario. 我在这里搜索了很多线程,但在上述情况下没有任何帮助。 Any help would be greatly appreciated as I have been stuck on this since very long time and this is now a showstopper!!!! 自从很长一段时间以来我一直在坚持这一点,现在任何时候都可以为我服务!

Looks like you are trying to make a call from a thread that is different from the thread the form is on. 看起来您正在尝试从与表单所在的线程不同的线程进行调用。 The events raised from the Process class will not be from the same thread. 从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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vb.net-将输出重定向到表单文本框 - vb.net - redirect output to form textbox 如何在不使用VB.net Windows窗体应用程序中的“”的情况下将Textbox值设置为double并使其为空? - How to set a Textbox value as double and make is empty without using “ ” in VB.net windows form application? 使用VB.net以编程方式更改Windows窗体上控件的位置? - Changing the location of a control on a windows form programatically using VB.net? 在Windows窗体vb.net中使用Append.Text之前,如何在文本框中打印文本行 - How to print text lines in a textbox before using Append.Text in windows form vb.net 如何在VB.Net Windows应用程序中找到动态创建的控件? - How to locate dynamically created control in VB.Net windows application? 如何使用VB.net获取Windows控制台应用程序的文本? - How do I get the text of a Windows console application using VB.net? 如何跨多个选项卡,Windows窗体,vb.net实时运行代码 - How to run code in real time across multiple tabs, windows form, vb.net 使用Ghostscript和VB.NET Windows Form Application在信封上打印 - Print on envelopes using Ghostscript with VB.NET Windows Form Application 如何在运行时Windows中使用vb.net将控件添加到其他应用程序的UI - how to add a control to other application's UI using vb.net in windows on runtime 如何从vb.net应用程序运行Windows命令“ mklink”? - How to run windows command “mklink” from vb.net application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM