描述C#调用外部进程

开发 后端
本文介绍C#调用外部进程这么简单的一件事究竟会有哪些问题,也希望我写的这个相对比较完整的类可以为软件开发的同道们节约一些脑细胞,以便集中优势兵力解决那些真正高深复杂的软件问题。

C#调用外部进程的类,网上可以搜出很多来,为什么要再写一遍,实在是因为最近从网上拷贝了一个简单的例程用到项目中,运行有问题,后来研究了半天,才解决了这些问题。于是打算写这么一篇博文,一来说说C#调用外部进程这么简单的一件事究竟会有哪些问题,二来也希望我写的这个相对比较完整的类可以为软件开发的同道们节约一些脑细胞,以便集中优势兵力解决那些真正高深复杂的软件问题。

在开始正题之前,我们先来看一看网上比较常见的执行外部进程的函数

  1. privatestringRunCmd(stringcommand)  
  2. {  
  3. //例Process  
  4. Processp=newProcess();  
  5.  
  6. p.StartInfo.FileName="cmd.exe";//确定程序名  
  7. p.StartInfo.Arguments="/c"+command;//确定程式命令行  
  8. p.StartInfo.UseShellExecute=false;//Shell的使用  
  9. p.StartInfo.RedirectStandardInput=true;//重定向输入  
  10. p.StartInfo.RedirectStandardOutput=true;//重定向输出  
  11. p.StartInfo.RedirectStandardError=true;//重定向输出错误  
  12. p.StartInfo.CreateNoWindow=true;//设置置不显示示窗口  
  13.  
  14. p.Start();//00  
  15.  
  16. //p.StandardInput.WriteLine(command);//也可以用这种方式输入入要行的命令  
  17. //p.StandardInput.WriteLine("exit");//要得加上Exit要不然下一行程式  
  18.  
  19. returnp.StandardOutput.ReadToEnd();//输出出流取得命令行结果果  
  20.  
  21. }  

这个方法应该是比较常见的C#调用外部进程的方法,我以前也一直是这样调用外部进程的,也没有碰到过什么问题。但这次调用的外部进程比较特殊,用这种方法调用就出现了两个问题。

***个问题是这个被调用的外部进程有时候会出现异常,出现异常后Windows会弹出错误报告框,程序于是吊死在那里,必须手工干预。这个问题比较好解决,程序中设置一下注册表搞定。

第二个问题是C#调用外部进程(是一个控制台进程)后,程序会阻塞在p.StandardOutput.ReadToEnd();这一句,永远无法出来,被调用的那个控制台程序也被吊死。但该控制台进程在CMD 中是可以正常执行的。后来看来一些资料才发现原来原因是出在该控制台程序控制台输出大量字符串,管道重定向后,调用程序没有及时将管道中的输出数据取出,结果导致管道被阻塞,程序吊死。在这里还有另外一个问题,虽然这次没有遇到,但网上有其他人遇到,就是错误信息管道不及时取出数据,也会被阻塞,而且如果要同时取出两个管道的数据,必须要利用一个辅助线程才能实现。

问题讲完了,下面给出这个类的完整代码

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Text;  
  4. usingSystem.Runtime.InteropServices;  
  5. usingSystem.Threading;  
  6.  
  7. namespaceLaboratory.Process  
  8. {  
  9. classReadErrorThread  
  10. {  
  11. System.Threading.Threadm_Thread;  
  12. System.Diagnostics.Processm_Process;  
  13. Stringm_Error;  
  14. boolm_HasExisted;  
  15. objectm_LockObj=newobject();  
  16.  
  17. publicStringError  
  18. {  
  19. get  
  20. {  
  21. returnm_Error;  
  22. }  
  23. }  
  24.  
  25. publicboolHasExisted  
  26. {  
  27. get  
  28. {  
  29. lock(m_LockObj)  
  30. {  
  31. returnm_HasExisted;  
  32. }  
  33. }  
  34.  
  35. set  
  36. {  
  37. lock(m_LockObj)  
  38. {  
  39. m_HasExisted=value;  
  40. }  
  41. }  
  42. }  
  43.  
  44. privatevoidReadError()  
  45. {  
  46. StringBuilderstrError=newStringBuilder();  
  47. while(!m_Process.HasExited)  
  48. {  
  49. strError.Append(m_Process.StandardError.ReadLine());  
  50. }  
  51.  
  52. strError.Append(m_Process.StandardError.ReadToEnd());  
  53.  
  54. m_Error=strError.ToString();  
  55. HasExisted=true;  
  56. }  
  57.  
  58. publicReadErrorThread(System.Diagnostics.Processp)  
  59. {  
  60. HasExisted=false;  
  61. m_Error="";  
  62. m_Process=p;  
  63. m_Thread=newThread(newThreadStart(ReadError));  
  64. m_Thread.Start();  
  65. }  
  66.  
  67. }  
  68.  
  69. classRunProcess  
  70. {  
  71. privateStringm_Error;  
  72. privateStringm_Output;  
  73.  
  74. publicStringError  
  75. {  
  76. get  
  77. {  
  78. returnm_Error;  
  79. }  
  80. }  
  81.  
  82. publicStringOutput  
  83. {  
  84. get  
  85. {  
  86. returnm_Output;  
  87. }  
  88. }  
  89.  
  90. publicboolHasError  
  91. {  
  92. get  
  93. {  
  94. returnm_Error!=""&&m_Error!=null;  
  95. }  
  96. }  
  97.  
  98. publicvoidRun(StringfileName,Stringpara)  
  99. {  
  100. StringBuilderoutputStr=newStringBuilder();  
  101.  
  102. try  
  103. {  
  104. //disabletheerrorreportdialog.  
  105. //reference:http://www.devcow.com/blogs/adnrg/archive/2006/07/14/
    Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx  
  106. Microsoft.Win32.RegistryKeykey;  
  107. key=Microsoft.Win32.Registry.LocalMachine.OpenSubKey
    (@"software\microsoft\PCHealth\ErrorReporting\",true);  
  108. intdoReport=(int)key.GetValue("DoReport");  
  109.  
  110. if(doReport!=0)  
  111. {  
  112. key.SetValue("DoReport",0);  
  113. }  
  114.  
  115. intshowUI=(int)key.GetValue("ShowUI");  
  116. if(showUI!=0)  
  117. {  
  118. key.SetValue("ShowUI",0);  
  119. }  
  120. }  
  121. catch  
  122. {  
  123. }  
  124.  
  125.  
  126. m_Error="";  
  127. m_Output="";  
  128. try  
  129. {  
  130. System.Diagnostics.Processp=newSystem.Diagnostics.Process();  
  131.  
  132. p.StartInfo.FileName=fileName;  
  133. p.StartInfo.Arguments=para;  
  134. p.StartInfo.UseShellExecute=false;  
  135. p.StartInfo.RedirectStandardInput=true;  
  136. p.StartInfo.RedirectStandardOutput=true;  
  137. p.StartInfo.RedirectStandardError=true;  
  138. p.StartInfo.CreateNoWindow=true;  
  139.  
  140. p.Start();  
  141.  
  142. ReadErrorThreadreadErrorThread=newReadErrorThread(p);  
  143.  
  144. while(!p.HasExited)  
  145. {  
  146. outputStr.Append(p.StandardOutput.ReadLine()+"\r\n");  
  147. }  
  148.  
  149. outputStr.Append(p.StandardOutput.ReadToEnd());  
  150.  
  151. while(!readErrorThread.HasExisted)  
  152. {  
  153. Thread.Sleep(1);  
  154. }  
  155.  
  156. m_Error=readErrorThread.Error;  
  157. m_Output=outputStr.ToString();  
  158. }  
  159. catch(Exceptione)  
  160. {  
  161. m_Error=e.Message;  
  162. }  
  163. }  
  164.  
  165. }  
  166. }  

【编辑推荐】

  1. 分析C#不安全代码
  2. 浅析C#调用ImageAnimator
  3. C#连接Access、SQL Server数据库
  4. 浅谈C#固定的和活动的变量
  5. 介绍C#中的值类型
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-07 17:19:50

C#调用外部进程

2009-09-03 17:59:18

C#调用事件

2009-09-03 16:20:14

C#调用Windows

2009-08-13 17:04:09

C#语言C#程序

2009-08-20 09:30:03

C#开发WinForm

2009-08-03 16:45:02

C#异步Socket

2009-08-17 16:32:34

C# Anonymou

2009-08-31 13:18:09

C# IWebMess

2009-08-03 18:08:39

C# ICloneab

2009-08-26 17:49:36

C# readonly

2009-08-31 18:32:01

C# ListBoxE

2009-08-18 17:41:22

C# ListView

2009-08-19 10:09:21

C#和C++

2009-09-07 15:31:49

C#支持事件

2009-09-07 13:02:52

Java和C#线程

2009-08-27 10:01:52

C#自动属性

2009-08-12 18:28:09

C#事件处理程序

2009-08-21 15:27:11

C# DataGrid

2009-09-03 16:55:58

C#引用类型

2009-08-20 16:45:03

C#哈希值
点赞
收藏

51CTO技术栈公众号