Processes and Output from Them

  • Thread starter Thread starter Chad Christensen
  • Start date Start date
C

Chad Christensen

Hello there,

I have the code to start a process, but what I don't understand is how to
spawn a
process and NOT have the main window hang where it was called from. In other
words, have it call a process and continue on and then let me know when it's
finished.

I also need to direct the standard output from the Perl program I'm calling
to something like an "output window" (whatever that would be) in real-time,
i.e. as it's coming from the Perl program instead of waiting until it's
finished.

Here is my code to start a process in Visual Studio .NET using C#. I found
this example on a web page and figured out how to call a Perl program. There
are a lot of problems with the Windows control the output is going to, i.e.
a MessageBox since it's not scrollable and shows one screen of the initial
messages. I need the messages shown as the Perl script is running and in
some kind of control that's scrollable and visible real-time to the users. I
am pretty sure this very thing has been done by many programmers, I just
need a little help putting it all together.

Here is the code I have already written:

public class SubProcess

{

public SubProcess()

{

//

// TODO: Add constructor logic here

//

}

public void call_process(string cmd, string args)

{

// Start a new process

Process p = new Process();

// Tells operating system not to use a shell

// Need this false to capture results in stdout

p.StartInfo.UseShellExecute = false;


// Allow me to capture stdout, i.e. results

p.StartInfo.RedirectStandardOutput = true;

// Allow me to capture stderr, i.e. results

p.StartInfo.RedirectStandardError = true;


// The command to invoke under MS-DOS

p.StartInfo.FileName = cmd;

// My command arguments

p.StartInfo.Arguments = args;

// Do not show MS-DOS window

p.StartInfo.CreateNoWindow = true;

// Do it!

p.Start();

// Capture any output results

string output = p.StandardOutput.ReadToEnd();

// Capture any error results

string error = p.StandardError.ReadToEnd();

// Wait for all results

p.WaitForExit();


int ret_code = p.ExitCode;

// Show results

if (ret_code == 0)

MessageBox.Show(output, "Program Results");

else

{

if (String.Equals(error, "") == true)

MessageBox.Show(output, "Program Results");

else MessageBox.Show(error, "Program Results");

}

}

}

Any help or insight would be greatly appreciated. :)

Chad Christensen
 
You should create and run the new process in a new thread.
This eliminates UI hanging.

Thread t = new Thread(new ThreadStart(this.StartMyNewProcess))
t.Start()

See the following posts:

This talks about recieveing an event when the process is terminated
http://weblogs.asp.net/rosherove/posts/8973.aspx

This talks about using output redirection(prety short, but it might help)
http://radio.weblogs.com/0117167/2003/06/19.html#a347


One more thing. If you'll be using a separate thread to handle the
termination event of the process, you'll need to take special care. If you
want to display something in the UI from a non-UI thread. , you need to call
Control.Invoke() instead of just setting textbox text and such. you'll get
unexpected results if a njon UI thread talks to UI controls.

See this post about this subject:
http://weblogs.asp.net/rosherove/posts/7269.aspx


--
Regards,

Roy Osherove
http://www.iserializable.com
---------------------------------------------
 
Back
Top