Results 1 to 5 of 5

Thread: [RESOLVED] How to run a process and get its output continously?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Resolved [RESOLVED] How to run a process and get its output continously?

    consoletest.exe

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i;
    
                for (i = 0; i < 10000; i++)
                    Console.Out.WriteLine(i.ToString());
    
    
    
    
    
            }
        }
    }

    windows project

    Code:
     private void button1_Click(object sender, EventArgs e)
            {
                System.Windows.Forms.Application.DoEvents();
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo();
    
    
                pi.FileName = "C:\\consoletest.exe";
                pi.Arguments = "C:\\mailbox";
                pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                pi.RedirectStandardOutput = true;
                pi.UseShellExecute = false;
    
                proc.StartInfo = pi;
                proc.Start();
                System.IO.StreamReader myOutput = proc.StandardOutput;
                           
    
    
                proc.WaitForExit();
                if (proc.HasExited)
                {
                    string output = myOutput.ReadToEnd();
                    textBox1.Text = output;
                }
            }
    The question is

    How do I run the console program in background and update its output in real time to the textbox?
    The windows program should also continue accepting other Events while the console process is being executed.
    Last edited by winterslam; Jul 12th, 2009 at 06:50 AM.

  2. #2
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: How to run a process and get its output continously?

    The way I see your code working atm is, the console only updates the textbox whenever the button is clicked.

    What you could do I suppose is create a service that will continuouly watch for changes on mailbox and as soon as there is a change on mailbox, it will run the console app and then update the textbox within the form for the service.

    The processes within the service will need to run in threads though and if you want the application to queue the events and process them as them come along, then use a threadpool to process it.

    Regards,
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: How to run a process and get its output continously?

    Sorry, it won't solve my problem..


    Anyone knows the solution?

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: How to run a process and get its output continously?

    This would be a great time to read the MSDN docs that have pretty useful examples:

    http://msdn.microsoft.com/en-us/libr...ardoutput.aspx

    http://msdn.microsoft.com/en-us/libr...treadline.aspx

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: How to run a process and get its output continously?

    Quote Originally Posted by axion_sa View Post
    This would be a great time to read the MSDN docs that have pretty useful examples:

    http://msdn.microsoft.com/en-us/libr...ardoutput.aspx

    http://msdn.microsoft.com/en-us/libr...treadline.aspx
    Thanks..
    Problem solved
    What I needed was just an event handler


    The following snippet is for the benefit of the people who might find this thread in the future
    Code:
      public partial class Form1 : Form
        {       
        
            public Form1()
            {
                InitializeComponent();
            }
            public void threadmethod()
            {
                Process process;
                process = new Process();
                process.StartInfo.FileName = "C:\\consoletest.exe"";
    
                
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;         
                           
                process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                          
                process.StartInfo.RedirectStandardInput = true;
    
             
                process.Start();
           
              
                process.BeginOutputReadLine();
    
                
               
                process.WaitForExit();      
                process.Close();
            }
    
            private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
            {           
                    SetTextBoxText(outLine.Data.ToString());            
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Thread thread1 = new Thread(threadmethod);
                thread1.Start();
    
            }
                   
    
    
          private delegate void SetTextBoxTextInvoker(string text); 
          private void SetTextBoxText(string text)   
          {
    
              if (this.textBox1.InvokeRequired) 
              {
                    this.textBox1.Invoke(new SetTextBoxTextInvoker(SetTextBoxText), text);  
              }  
              else
              { 
                  this.textBox1.Text = text;
              } 
          } 
    
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width