Writing redirected output from an unmanaged executable to a windows form control
-
hi guys, this question related to my previous post: Link The current problem is redirecting the output to a textbox... there is the issue of a thread different from form's thread, trying to access the textbox.text property (i've read an article in msdn, i which they query the invokerequired property of the textbox and invoke it same method using this.invoke, but it doesn't seem to solve the problem) again, i want to start a new process and redirect its output to a textbox at runtime (every line of text written by the process should be written at runtime at the textbox, without waiting for the process to finish)... the answer in the first reply i got helped, but i couldn't make it thread safe (and in the buttom line i think it wrote the output to the textbox, but the window couldn't refresh itself until the other process has finished... any suggestions? Thanks in advance, Erik.
-
hi guys, this question related to my previous post: Link The current problem is redirecting the output to a textbox... there is the issue of a thread different from form's thread, trying to access the textbox.text property (i've read an article in msdn, i which they query the invokerequired property of the textbox and invoke it same method using this.invoke, but it doesn't seem to solve the problem) again, i want to start a new process and redirect its output to a textbox at runtime (every line of text written by the process should be written at runtime at the textbox, without waiting for the process to finish)... the answer in the first reply i got helped, but i couldn't make it thread safe (and in the buttom line i think it wrote the output to the textbox, but the window couldn't refresh itself until the other process has finished... any suggestions? Thanks in advance, Erik.
Hello Your problem -I presume- is that your thread waits for the process to finish until it views the output in the textbox? You should use
Process.BeginOutputReadLine()
method, and it should work fine. If you are using it, what seems to be the problem again? About theInvoke()
, it should also work fine if you use theProcess.BeginOutputReadLine()
method. If you still can't get it right post your code and maybe we could work it out.Regards:rose:
-
hi guys, this question related to my previous post: Link The current problem is redirecting the output to a textbox... there is the issue of a thread different from form's thread, trying to access the textbox.text property (i've read an article in msdn, i which they query the invokerequired property of the textbox and invoke it same method using this.invoke, but it doesn't seem to solve the problem) again, i want to start a new process and redirect its output to a textbox at runtime (every line of text written by the process should be written at runtime at the textbox, without waiting for the process to finish)... the answer in the first reply i got helped, but i couldn't make it thread safe (and in the buttom line i think it wrote the output to the textbox, but the window couldn't refresh itself until the other process has finished... any suggestions? Thanks in advance, Erik.
you are familiar to "..." block same as my first reply ... System.Diagnostics.Process pr = new System.Diagnostics.Process(); pr.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; pr.StartInfo.FileName = "cmd.exe"; pr.StartInfo.UseShellExecute = false; pr.StartInfo.RedirectStandardInput = true; pr.StartInfo.RedirectStandardOutput = true; pr.Start(); pr.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(pr_OutputDataReceived); pr.BeginOutputReadLine(); System.IO.StreamWriter swWriter = pr.StandardInput; swWriter.WriteLine("echo SomeTextHere\n"); swWriter.Flush(); ... //Define a delagate function delegate void SetTextCallback(string text); //Define a function for setting void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } } void pr_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { //event fired and u have data use it thread-safe SetText(e.Data); } if this does not work define your problem with more details
UMS
-
you are familiar to "..." block same as my first reply ... System.Diagnostics.Process pr = new System.Diagnostics.Process(); pr.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; pr.StartInfo.FileName = "cmd.exe"; pr.StartInfo.UseShellExecute = false; pr.StartInfo.RedirectStandardInput = true; pr.StartInfo.RedirectStandardOutput = true; pr.Start(); pr.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(pr_OutputDataReceived); pr.BeginOutputReadLine(); System.IO.StreamWriter swWriter = pr.StandardInput; swWriter.WriteLine("echo SomeTextHere\n"); swWriter.Flush(); ... //Define a delagate function delegate void SetTextCallback(string text); //Define a function for setting void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } } void pr_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { //event fired and u have data use it thread-safe SetText(e.Data); } if this does not work define your problem with more details
UMS