C# 調用外部進程的類
在開始正題之前
private string RunCmd(string command)
{
//例Process
Process p = new Process();
p
p
p
p
p
p
p
p
//p
//p
return p
}
這個方法應該是比較常見的調用外部進程的方法
第一個問題是這個被調用的外部進程有時候會出現異常
第二個問題是調用這個外部進程(是一個控制台進程)後
問題講完了
using System;
using System
using System
using System
using System
namespace Laboratory
{
class ReadErrorThread
{
System
System
String m_Error;
bool m_HasExisted;
object m_LockObj = new object();
public String Error
{
get
{
return m_Error;
}
}
public bool HasExisted
{
get
{
lock (m_LockObj)
{
return m_HasExisted;
}
}
set
{
lock (m_LockObj)
{
m_HasExisted = value;
}
}
}
private void ReadError()
{
StringBuilder strError = new StringBuilder();
while (!m_Process
{
strError
}
strError
m_Error = strError
HasExisted = true;
}
public ReadErrorThread(System
{
HasExisted = false;
m_Error =
m_Process = p;
m_Thread = new Thread(new ThreadStart(ReadError));
m_Thread
}
}
class RunProcess
{
private String m_Error;
private String m_Output;
public String Error
{
get
{
return m_Error;
}
}
public String Output
{
get
{
return m_Output;
}
}
public bool HasError
{
get
{
return m_Error !=
}
}
public void Run(String fileName
{
StringBuilder outputStr = new StringBuilder();
try
{
//disable the error report dialog
//reference: http://www
Microsoft
key = Microsoft
int doReport = (int)key
if (doReport !=
{
key
}
int showUI = (int)key
if (showUI !=
{
key
}
}
catch
{
}
m_Error =
m_Output =
try
{
System
p
p
p
p
p
p
p
p
ReadErrorThread readErrorThread = new ReadErrorThread(p);
while (!p
{
outputStr
}
outputStr
while (!readErrorThread
{
Thread
}
m_Error = readErrorThread
m_Output = outputStr
}
catch (Exception e)
{
m_Error = e
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/15678.html