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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  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#
comtoolshelpquestion
4 Posts 2 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 again guys, still same problem, this time i'll post my code: (the previous thread : Link m_prsAlgorithm = new Process(); m_prsAlgorithm.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; m_prsAlgorithm.StartInfo.FileName = "Lottery.exe"; m_prsAlgorithm.StartInfo.UseShellExecute = false; m_prsAlgorithm.StartInfo.RedirectStandardInput = true; m_prsAlgorithm.StartInfo.RedirectStandardOutput = true; m_prsAlgorithm.OutputDataReceived += new DataReceivedEventHandler(prsAlgorithm_OutputDataReceived); m_prsAlgorithm.Start(); m_prsAlgorithm.BeginOutputReadLine(); System.IO.StreamWriter swWriter = m_prsAlgorithm.StandardInput; swWriter.WriteLine(m_strArgList + " d"); swWriter.Close(); m_prsAlgorithm.WaitForExit(); } void prsAlgorithm_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { this.txtOutput.Text += e.Data; //this.SetText(e.Data); } the code above couses my program to freeze (just hangs), if i uncomment the code in void prsAlgorithm_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) and comment out the: this.txtOutput.Text += e.Data; it works but it's not thread safe and it only shows me the output when the other process has finished... any suggestions? Thanks in advance, Erik.

    L 1 Reply Last reply
    0
    • E erikash

      hi again guys, still same problem, this time i'll post my code: (the previous thread : Link m_prsAlgorithm = new Process(); m_prsAlgorithm.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; m_prsAlgorithm.StartInfo.FileName = "Lottery.exe"; m_prsAlgorithm.StartInfo.UseShellExecute = false; m_prsAlgorithm.StartInfo.RedirectStandardInput = true; m_prsAlgorithm.StartInfo.RedirectStandardOutput = true; m_prsAlgorithm.OutputDataReceived += new DataReceivedEventHandler(prsAlgorithm_OutputDataReceived); m_prsAlgorithm.Start(); m_prsAlgorithm.BeginOutputReadLine(); System.IO.StreamWriter swWriter = m_prsAlgorithm.StandardInput; swWriter.WriteLine(m_strArgList + " d"); swWriter.Close(); m_prsAlgorithm.WaitForExit(); } void prsAlgorithm_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { this.txtOutput.Text += e.Data; //this.SetText(e.Data); } the code above couses my program to freeze (just hangs), if i uncomment the code in void prsAlgorithm_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) and comment out the: this.txtOutput.Text += e.Data; it works but it's not thread safe and it only shows me the output when the other process has finished... any suggestions? Thanks in advance, Erik.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      prsAlgorithm_OutputDataReceived might be executing on a different thread so you need to check and Invoke the UI thread call to transfer the data.

      led mike

      E 1 Reply Last reply
      0
      • L led mike

        prsAlgorithm_OutputDataReceived might be executing on a different thread so you need to check and Invoke the UI thread call to transfer the data.

        led mike

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

        hi guys, finally i have something that works: private void RunAlgorithm() { m_prsAlgorithm = new Process(); m_prsAlgorithm.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; m_prsAlgorithm.StartInfo.FileName = "Lottery.exe"; m_prsAlgorithm.StartInfo.UseShellExecute = false; m_prsAlgorithm.StartInfo.RedirectStandardInput = true; m_prsAlgorithm.StartInfo.RedirectStandardOutput = true; m_prsAlgorithm.StartInfo.CreateNoWindow = true; m_prsAlgorithm.OutputDataReceived += new DataReceivedEventHandler(prsAlgorithm_OutputDataReceived); m_prsAlgorithm.Start(); m_prsAlgorithm.BeginOutputReadLine(); System.IO.StreamWriter swWriter = m_prsAlgorithm.StandardInput; swWriter.WriteLine(m_strArgList); swWriter.Close(); m_prsAlgorithm.WaitForExit(); ProcessExitedCallback pecProcessExited = new ProcessExitedCallback(OnProcessExited); m_prsAlgorithm.WaitForExit(); btnRun.Invoke(pecProcessExited, new object[] { }); } i've inserted this into a function and called this function in a new thread (from the main thread) and it seems to work like a charm... :) does anyone know why this solved the problem? Thanks in advance, Erik.

        E 1 Reply Last reply
        0
        • E erikash

          hi guys, finally i have something that works: private void RunAlgorithm() { m_prsAlgorithm = new Process(); m_prsAlgorithm.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; m_prsAlgorithm.StartInfo.FileName = "Lottery.exe"; m_prsAlgorithm.StartInfo.UseShellExecute = false; m_prsAlgorithm.StartInfo.RedirectStandardInput = true; m_prsAlgorithm.StartInfo.RedirectStandardOutput = true; m_prsAlgorithm.StartInfo.CreateNoWindow = true; m_prsAlgorithm.OutputDataReceived += new DataReceivedEventHandler(prsAlgorithm_OutputDataReceived); m_prsAlgorithm.Start(); m_prsAlgorithm.BeginOutputReadLine(); System.IO.StreamWriter swWriter = m_prsAlgorithm.StandardInput; swWriter.WriteLine(m_strArgList); swWriter.Close(); m_prsAlgorithm.WaitForExit(); ProcessExitedCallback pecProcessExited = new ProcessExitedCallback(OnProcessExited); m_prsAlgorithm.WaitForExit(); btnRun.Invoke(pecProcessExited, new object[] { }); } i've inserted this into a function and called this function in a new thread (from the main thread) and it seems to work like a charm... :) does anyone know why this solved the problem? Thanks in advance, Erik.

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

          ok this almost solved the problem ;) The program works perfectly if the process executed runs for a short amount of time, but if that process works a couple of minutes than it takes a pretty long time for the redirected output to show in the textbox (output in the other process is written continuously while calculating) any suggestions? Thanks in advance, 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