Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Saturday, June 16, 2012

Run another exe process in C#

Run another exe process in C# Sometimes, we may want to call other programs to do something in our program before going to do next step.
In C#, simply, by calling Process class in System.Diagnostics namespace.
This class has a lot of features as following examples:
- Enable/Disable new Window to launch new program.
- Redirect input/output and error stream to the main program.
- Run a process in synchronous or asynchronous.
- Receive arguments

Basically, I classify how to use this class into 2 styles, synchronous and asynchronous.
Mostly, if I am working with GUI application, I will use asynchronous way because it allows me to get an output in each time when there is the output come from another applciation, also it will not block the main program to wait for the entire output. However, If you do asynchronously in console application, you might not see the difference between synchronous and asynchronous because asynchronous method will run a process as redirect standard output that it won't open the new console window (if the main program is a console application).

To use Process class, there are few steps to set up as follows:
1. Create ProcessStartInfo instance.
2. Set up its properties to initiate Process.
3. Create Process instance using ProcessStartInfo setup environment.
4. Start Process

To run with synchronous in a new window, you can setup as following code:
String directory = "somewhere";
String fileName = directory + "filename.exe";

ProcessStartInfo info = new ProcessStartInfo()
{
 WorkingDirectory = directory,
 FileName = fileName
};

Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForExit(); // Main program will wait for the sub process
However, to run synchronous in the same window you can just add some properties into ProcessStartInfo instance.
ProcessStartInfo info = new ProcessStartInfo()
{
 WorkingDirectory = directory,
 FileName = fileName,
 RedirectStandardOutput = true,
 UseShellExecute = false,
 CreateNoWindow = true // It is needed when yor main program is GUI beacause it will open cmd window if it is false.
};

Process p = new Process();
p.StartInfo = info;
p.Start();
// Not necessary beacuse it will automatically wait for synchonize the output stream.
p.WaitForExit();
String output = p.StandardOutput.ReadToEnd(); // If your main program is GUI, your GUI thread will wait because of this line.
To run asynchronous, you just do not use "StandardOutput.ReadToEnd", according to the same reason in the comment above. You need to use DataReceivedEventHandler instead that this event will be fired when there is a data comming out from the sub process, then in order to use it, the other settings are the same to previous example.
ProcessStartInfo info = new ProcessStartInfo()
{
 WorkingDirectory = directory,
 FileName = fileName,
 RedirectStandardOutput = true,
 UseShellExecute = false,
 CreateNoWindow = true // When it is false in the main GUI program, it will open cmd window but the output will be redirected to main program.
};

Process p = new Process();
p.StartInfo = info;
p.OutputDataReceived +=
 (o, e) =>
 {
  String output = e.Data;
  Console.WriteLine(output);
 };
p.Start();
p.BeginOutputReadLine(); // You need to add this line to tell the sub process that you start to listen data output asynchronously.
In my opinion, these examples are enough to introduce main featues of Process class. To add arguments or redirect input stream, I think you can find it from other resources easily. Hope you enjoy to read and use from this post :).

More information at: http://www.codeproject.com/Articles/302015/Running-Any-Command-Line-exe-Remotely-Using-Proceshttp://www.codeproject.com/Articles/170017/Solving-Problems-of-Monitoring-Standard-Output-and

No comments:

Post a Comment