C # call an external process of the class, on-line can be found many years, why write it again, it is because the most recent copy from the Internet, the routine use of a simple project, operation problems, and later studied for half a day to solve the these problems. He intended to write such a blog post, one for the C # call an external process that such a simple thing really what the issue, and secondly, want me to write this class of relatively complete software development can save some fellow who brain cells in order to concentrate superior forces to solve the really highly complex software problems.
Before going to the subject at the beginning, let's take a look at the implementation of the more common Internet functions external process
privatestringRunCmd (stringcommand)
(
/ / Case of Process
Processp = newProcess ();
p.StartInfo.FileName = "cmd.exe"; / / determine the name of the program
p.StartInfo.Arguments = "/ c" + command; / / Ensure that the program command line
p.StartInfo.UseShellExecute = false; / / Shell's use of
p.StartInfo.RedirectStandardInput = true; / / Redirect input
p.StartInfo.RedirectStandardOutput = true; / / redirect output
p.StartInfo.RedirectStandardError = true; / / redirect output error
p.StartInfo.CreateNoWindow = true; / / set the display window does not display home
p.Start ();// 00
/ / P.StandardInput.WriteLine (command); / / in this way can also be used to enter into the command line
/ / P.StandardInput.WriteLine ("exit ");// quest with Exit or else the next line of code
returnp.StandardOutput.ReadToEnd ();// get the output from the command line results flow results
) This method should be more common C # call an external process, and I have been so before calling the external process, not encountered any problems. But the call of the external process is rather special, with this method call on the emergence of two problems.
The first is that the external process is sometimes called abnormal, abnormal after the Windows error reporting pop up box, the program was hanging in there, to be manual intervention. Better to solve this problem, the program set about registry buttoned.
The second problem is that C # call an external process (a console process), the program will block the p.StandardOutput.ReadToEnd (); this one, will never come out, is called the console application was hanged. However, the console process can be normal in the CMD is executed. Some information later on found out that because the program console output in the console a lot of strings, pipe redirection, call the program does not promptly remove the pipe in the output data, resulting pipeline is blocked, the program hang. There is another issue here, although this did not encounter, but there are other people experience the Internet is the error message is not promptly remove the data channel will be blocked, but if you want to remove the two channels of data simultaneously, must be can be achieved using a secondary thread.
After describing the problem, here are the complete code for this class
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
usingSystem.Threading;
namespaceLaboratory.Process
(
classReadErrorThread
(
System.Threading.Threadm_Thread;
System.Diagnostics.Processm_Process;
Stringm_Error;
boolm_HasExisted;
objectm_LockObj = newobject ();
publicStringError
(
get
(
returnm_Error;
)
)
publicboolHasExisted
(
get
(
lock (m_LockObj)
(
returnm_HasExisted;
)
)
set
(
lock (m_LockObj)
(
m_HasExisted = value;
)
)
)
privatevoidReadError ()
(
StringBuilderstrError = newStringBuilder ();
while (! m_Process.HasExited)
(
strError.Append (m_Process.StandardError.ReadLine ());
)
strError.Append (m_Process.StandardError.ReadToEnd ());
m_Error = strError.ToString ();
HasExisted = true;
)
publicReadErrorThread (System.Diagnostics.Processp)
(
HasExisted = false;
m_Error = "";
m_Process = p;
m_Thread = newThread (newThreadStart (ReadError));
m_Thread.Start ();
)
)
classRunProcess
(
privateStringm_Error;
privateStringm_Output;
publicStringError
(
get
(
returnm_Error;
)
)
publicStringOutput
(
get
(
returnm_Output;
)
)
publicboolHasError
(
get
(
returnm_Error !=""&& m_Error! = null;
)
)
publicvoidRun (StringfileName, Stringpara)
(
StringBuilderoutputStr = newStringBuilder ();
try
(
/ / Disabletheerrorreportdialog.
/ / Reference: http://www.devcow.com/blogs/adnrg/archive/2006/07/14/
Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx
Microsoft.Win32.RegistryKeykey;
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey
(@ "SoftwaremicrosoftPCHealthErrorReporting", true);
intdoReport = (int) key.GetValue ("DoReport");
if (doReport! = 0)
(
key.SetValue ("DoReport", 0);
)
intshowUI = (int) key.GetValue ("ShowUI");
if (showUI! = 0)
(
key.SetValue ("ShowUI", 0);
)
)
catch
(
)
m_Error = "";
m_Output = "";
try
(
System.Diagnostics.Processp = newSystem.Diagnostics.Process ();
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = para;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start ();
ReadErrorThreadreadErrorThread = newReadErrorThread (p);
while (! p.HasExited)
(
outputStr.Append (p.StandardOutput.ReadLine () + "
");
)
outputStr.Append (p.StandardOutput.ReadToEnd ());
while (! readErrorThread.HasExisted)
(
Thread.Sleep (1);
)
m_Error = readErrorThread.Error;
m_Output = outputStr.ToString ();
)
catch (Exceptione)
(
m_Error = e.Message;
)
)
)
)
How to call Servlet to process the request
Bug tracking process issues that need attention
oracle10g r2 emca command and examples of the
Guide Food And Drink
Qicktime to WMV
Specialist Benchmarking
XviD TO WMV
First job out of the ivory tower beginning how the ELECTION
Thoroughly Transform UML 2.0
MOV To WMV
PHOTOSHOP Gourmet Series peanut butter cookies
Expert Personal Interest