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
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