How to redirect Output of console application to Textbox control on a windows form at run time?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
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. 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,
<ol start="1" type="1" style="
Create the process instance of this serverLaunch the Server with appropriate parameters and keep it running with button event. Below is some sample code from my application to launch the server,</ol>
<span style="font-size:9.5pt; font-family:Consolas <span style="font-size:9.5pt; font-family:Consolas; color:blue Private<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Sub<span style="font-size:9.5pt; font-family:Consolas Server_Start_Click(sender
<span style="font-size:9.5pt; font-family:Consolas; color:blue As<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Object<span style="font-size:9.5pt; font-family:Consolas , e
<span style="font-size:9.5pt; font-family:Consolas; color:blue As<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:#2b91af EventArgs<span style="font-size:9.5pt; font-family:Consolas )
<span style="font-size:9.5pt; font-family:Consolas; color:blue Handles<span style="font-size:9.5pt; font-family:Consolas Server_Start.Click
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Dim<span style="font-size:9.5pt; font-family:Consolas parameter, server_admin_path
<span style="font-size:9.5pt; font-family:Consolas; color:blue As<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue String
<span style="font-size:9.5pt; font-family:Consolas server_admin_path =
<span style="font-size:9.5pt; font-family:Consolas; color:#a31515 "D:Voice_AppDataMessageMessageServer.exe"
<span style="font-size:9.5pt; font-family:Consolas parameter =
<span style="font-size:9.5pt; font-family:Consolas; color:#a31515 " -properties "<span style="font-size:9.5pt; font-family:Consolas &
<span style="font-size:9.5pt; font-family:Consolas; color:#a31515 """"<span style="font-size:9.5pt; font-family:Consolas &
<span style="font-size:9.5pt; font-family:Consolas; color:#a31515 " D:Voice_AppConfigmessage.prop"

<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Dim<span style="font-size:9.5pt; font-family:Consolas proc =
<span style="font-size:9.5pt; font-family:Consolas; color:blue New<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:#2b91af Process<span style="font-size:9.5pt; font-family:Consolas ()
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.FileName = server_admin_path
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.Arguments = parameter
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green set up output redirection
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.RedirectStandardOutput =
<span style="font-size:9.5pt; font-family:Consolas; color:blue True
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.RedirectStandardError =
<span style="font-size:9.5pt; font-family:Consolas; color:blue True
<span style="font-size:9.5pt; font-family:Consolas proc.EnableRaisingEvents =
<span style="font-size:9.5pt; font-family:Consolas; color:blue True
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:#2b91af Application<span style="font-size:9.5pt; font-family:Consolas .DoEvents()
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.CreateNoWindow =
<span style="font-size:9.5pt; font-family:Consolas; color:blue False
<span style="font-size:9.5pt; font-family:Consolas proc.StartInfo.UseShellExecute =
<span style="font-size:9.5pt; font-family:Consolas; color:blue False
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green see below for output handler
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue AddHandler<span style="font-size:9.5pt; font-family:Consolas proc.ErrorDataReceived,
<span style="font-size:9.5pt; font-family:Consolas; color:blue AddressOf<span style="font-size:9.5pt; font-family:Consolas proc_OutputDataReceived
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue AddHandler<span style="font-size:9.5pt; font-family:Consolas proc.OutputDataReceived,
<span style="font-size:9.5pt; font-family:Consolas; color:blue AddressOf<span style="font-size:9.5pt; font-family:Consolas proc_OutputDataReceived
<span style="font-size:9.5pt; font-family:Consolas proc.Start()
<span style="font-size:9.5pt; font-family:Consolas proc.BeginErrorReadLine()
<span style="font-size:9.5pt; font-family:Consolas proc.BeginOutputReadLine()
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green proc.WaitForExit()
<span style="font-size:9.5pt; font-family:Consolas Server_Logs.Focus()
<span style="font-size:9.5pt; font-family:Consolas 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. 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 in to the Textbox I have on my windows form.
I got below code which gives me that every line as and when produced in message box,
<span style="font-size:9.5pt; font-family:Consolas; color:blue Public<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Sub<span style="font-size:9.5pt; font-family:Consolas proc_OutputDataReceived(<span style="font-size:9.5pt; font-family:Consolas; color:blue ByVal<span style="font-size:9.5pt; font-family:Consolas
sender <span style="font-size:9.5pt; font-family:Consolas; color:blue As<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Object<span style="font-size:9.5pt; font-family:Consolas ,
<span style="font-size:9.5pt; font-family:Consolas; color:blue ByVal<span style="font-size:9.5pt; font-family:Consolas e
<span style="font-size:9.5pt; font-family:Consolas; color:blue As<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:#2b91af DataReceivedEventArgs<span style="font-size:9.5pt; font-family:Consolas )
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue On<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Error<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Resume<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Next
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green output will be in string e.Data
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green modify TextBox.Text here
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:green Server_Logs.Text = e.Data ` Does not display anything in textbox
<span style="font-size:9.5pt; font-family:Consolas MsgBox(e.Data)
<span style="font-size:9.5pt; font-family:Consolas; color:green It works but I want output in text box field
<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue End<span style="font-size:9.5pt; font-family:Consolas
<span style="font-size:9.5pt; font-family:Consolas; color:blue Sub

<span style="font-size:11.0pt; font-family:Calibri P.S = 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.
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 show stopper!!!!

View the full article
 
Back
Top