偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

描述C#調用外部進程

開發(fā) 后端
本文介紹C#調用外部進程這么簡單的一件事究竟會有哪些問題,也希望我寫的這個相對比較完整的類可以為軟件開發(fā)的同道們節(jié)約一些腦細胞,以便集中優(yōu)勢兵力解決那些真正高深復雜的軟件問題。

C#調用外部進程的類,網上可以搜出很多來,為什么要再寫一遍,實在是因為最近從網上拷貝了一個簡單的例程用到項目中,運行有問題,后來研究了半天,才解決了這些問題。于是打算寫這么一篇博文,一來說說C#調用外部進程這么簡單的一件事究竟會有哪些問題,二來也希望我寫的這個相對比較完整的類可以為軟件開發(fā)的同道們節(jié)約一些腦細胞,以便集中優(yōu)勢兵力解決那些真正高深復雜的軟件問題。

在開始正題之前,我們先來看一看網上比較常見的執(zhí)行外部進程的函數(shù)

  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#調用外部進程的方法,我以前也一直是這樣調用外部進程的,也沒有碰到過什么問題。但這次調用的外部進程比較特殊,用這種方法調用就出現(xiàn)了兩個問題。

***個問題是這個被調用的外部進程有時候會出現(xiàn)異常,出現(xiàn)異常后Windows會彈出錯誤報告框,程序于是吊死在那里,必須手工干預。這個問題比較好解決,程序中設置一下注冊表搞定。

第二個問題是C#調用外部進程(是一個控制臺進程)后,程序會阻塞在p.StandardOutput.ReadToEnd();這一句,永遠無法出來,被調用的那個控制臺程序也被吊死。但該控制臺進程在CMD 中是可以正常執(zhí)行的。后來看來一些資料才發(fā)現(xiàn)原來原因是出在該控制臺程序控制臺輸出大量字符串,管道重定向后,調用程序沒有及時將管道中的輸出數(shù)據(jù)取出,結果導致管道被阻塞,程序吊死。在這里還有另外一個問題,雖然這次沒有遇到,但網上有其他人遇到,就是錯誤信息管道不及時取出數(shù)據(jù),也會被阻塞,而且如果要同時取出兩個管道的數(shù)據(jù),必須要利用一個輔助線程才能實現(xiàn)。

問題講完了,下面給出這個類的完整代碼

  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數(shù)據(jù)庫
  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-03 16:45:02

C#異步Socket

2009-08-20 09:30:03

C#開發(fā)WinForm

2009-08-17 16:32:34

C# Anonymou

2009-08-18 17:41:22

C# ListView

2009-08-31 13:18:09

C# IWebMess

2009-08-26 17:49:36

C# readonly

2009-08-03 18:08:39

C# ICloneab

2009-08-31 18:32:01

C# ListBoxE

2009-08-19 10:09:21

C#和C++

2024-08-13 08:25:16

C#外部程序方式

2009-08-12 18:28:09

C#事件處理程序

2009-08-27 10:01:52

C#自動屬性

2009-09-01 17:08:35

C# Color枚舉

2009-09-03 16:55:58

C#引用類型

2009-09-07 13:02:52

Java和C#線程

2009-09-07 15:31:49

C#支持事件
點贊
收藏

51CTO技術棧公眾號