Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Writing redirected output from an unmanaged executable to a windows form control

Writing redirected output from an unmanaged executable to a windows form control

Scheduled Pinned Locked Moved C#
helpquestiondatabasecomtools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    erikash
    wrote on last edited by
    #1

    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.

    N U 2 Replies Last reply
    0
    • E erikash

      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.

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      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 the Invoke(), it should also work fine if you use the Process.BeginOutputReadLine() method. If you still can't get it right post your code and maybe we could work it out.

      Regards:rose:

      1 Reply Last reply
      0
      • E erikash

        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.

        U Offline
        U Offline
        umseker
        wrote on last edited by
        #3

        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

        E 1 Reply Last reply
        0
        • U umseker

          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

          E Offline
          E Offline
          erikash
          wrote on last edited by
          #4

          Thanks for the replies guys, umseker thats almost exacly what i wrote... the problem is that the window doesn't repaint itself until the other process has finished (i presume). Anyway i'll post the code when i'll get home... Erik.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups