声明并启动如下流程后:
System.Diagnostics.Process _p = new System.Diagnostics.Process();
.....
.....
....
_p.Start();
现在有两种可能性:输出或错误。
如果发生错误,是否有任何Process
类的属性可以知道是否发生了错误?
我正在重定向标准输出,我不想像MSDN 中警告的那样重定向标准错误。我也不想使用:BeginOutputReadLine();
有没有其他选择?
谢谢你。
声明并启动如下流程后:
System.Diagnostics.Process _p = new System.Diagnostics.Process();
.....
.....
....
_p.Start();
现在有两种可能性:输出或错误。
如果发生错误,是否有任何Process
类的属性可以知道是否发生了错误?
我正在重定向标准输出,我不想像MSDN 中警告的那样重定向标准错误。我也不想使用:BeginOutputReadLine();
有没有其他选择?
谢谢你。
我有一个服务需要启动进程并等待它们退出,我使用类似的东西:
process.Start();
int timeout = ... // some normal value in milliseconds
process.WaitForExit(timeout);
try
{
//ExitCode throws if the process is hanging
return (CommandErrorCode)process.ExitCode;
}
catch (InvalidOperationException ioex)
{
return CommandErrorCode.InternalError;
}
CommandErrorCode
类似的东西在哪里
public enum CommandErrorCode
{
Success = 0,
//some other values I know from the processes that are managed
InternalError = 256 // the ExitCode is a byte, so this out of that range
}
顺便说一句,我重定向标准输出和标准错误,并使用 BeginXXXReadLine 和 XXXDataReceived 处理程序,没有任何问题,但我使用的过程是已知的、定义明确的和行为良好的。
这是一个代码片段,希望这有帮助
http://ss64.com/nt/cmd.html获取 cmd.exe 帮助
private int CallShell(string exeCommand, string Parameters)
{
//http://ss64.com/nt/cmd.html
/*
This function will actually take the shell string and envoke the appropriate process
passing it the arguments to do the work
*/
// Initialize the process and its StartInfo properties.
System.Diagnostics.Process ProcessEXE = new System.Diagnostics.Process();
logger.DebugFormat("About to Start Process - {0} {1}",exeCommand, Parameters);
try
{
ProcessEXE.StartInfo.FileName = exeCommand;
// Set UseShellExecute to false for redirection.
// false if the process should be created directly from the executable file
ProcessEXE.StartInfo.UseShellExecute = false;
ProcessEXE.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory;
//EnableRaisingEvents property indicates whether the component should be notified when the operating system has shut down a process
ProcessEXE.StartInfo.Arguments = Parameters;
ProcessEXE.StartInfo.RedirectStandardOutput = true;
ProcessEXE.StartInfo.RedirectStandardError = true;
ProcessEXE.EnableRaisingEvents = true;
ProcessEXE.StartInfo.CreateNoWindow = true;
ProcessEXE.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(ProcessEXE_OutputDataReceived);
ProcessEXE.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(ProcessEXE_OutputDataReceived);
logger.DebugFormat("Process Started.");
ProcessEXE.Start();
// Start the asynchronous read of the sort output stream.
ProcessEXE.BeginErrorReadLine();
ProcessEXE.BeginOutputReadLine();
//The WaitForExit overload is used to make the current thread wait until the associated process terminates
logger.DebugFormat("Process Waiting for exit.");
ProcessEXE.WaitForExit();
if (ProcessEXE.ExitCode == 0)
{
logger.Debug(string.Format("Shell Process exited with exit code {0}", ProcessEXE.ExitCode));
}
else
{
// throw error here if required - check the return error code
logger.Warn(string.Format("Shell Process exited with exit code {0}", ProcessEXE.ExitCode));
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Method:{0}", ex.TargetSite), ex);
}
return ProcessEXE.ExitCode;
}
void ProcessEXE_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
try
{
StringBuilder sb = new StringBuilder(string.Empty);
if (e == null) return;
if (e.Data == null)
{
//No processing
}
else
{
// your logic to detect error msg out from output - if at all required
}
if (sb.ToString().ToUpper().IndexOf("ERROR") > 0)
{
string smessage = "Error text found in output.";
// do your error response action here .
}
}
catch (Exception exp)
{
logger.ErrorFormat("Error in ProcessEXE_OutputDataReceived Message:{0}", exp.Message);
logger.ErrorFormat("Error in ProcessEXE_OutputDataReceived Data Received:{0}", e.Data);
// either throw error msg or kill the process
}
finally
{
// Not throwing the exception
}
}