日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
描述C#調(diào)用外部進程

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

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:國際域名空間、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設、琿春網(wǎng)站維護、網(wǎng)站推廣。

在開始正題之前,我們先來看一看網(wǎng)上比較常見的執(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#調(diào)用外部進程的方法,我以前也一直是這樣調(diào)用外部進程的,也沒有碰到過什么問題。但這次調(diào)用的外部進程比較特殊,用這種方法調(diào)用就出現(xiàn)了兩個問題。

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

第二個問題是C#調(diào)用外部進程(是一個控制臺進程)后,程序會阻塞在p.StandardOutput.ReadToEnd();這一句,永遠無法出來,被調(diào)用的那個控制臺程序也被吊死。但該控制臺進程在CMD 中是可以正常執(zhí)行的。后來看來一些資料才發(fā)現(xiàn)原來原因是出在該控制臺程序控制臺輸出大量字符串,管道重定向后,調(diào)用程序沒有及時將管道中的輸出數(shù)據(jù)取出,結果導致管道被阻塞,程序吊死。在這里還有另外一個問題,雖然這次沒有遇到,但網(wǎng)上有其他人遇到,就是錯誤信息管道不及時取出數(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#調(diào)用ImageAnimator
  3. C#連接Access、SQL Server數(shù)據(jù)庫
  4. 淺談C#固定的和活動的變量
  5. 介紹C#中的值類型

當前文章:描述C#調(diào)用外部進程
文章位置:http://www.dlmjj.cn/article/cdddjei.html