溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#管道技術(shù)怎么實現(xiàn)

發(fā)布時間:2021-12-02 11:36:19 來源:億速云 閱讀:240 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“C#管道技術(shù)怎么實現(xiàn)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

首先,我們可以通過設(shè)置Process類,獲取輸出接口,代碼如下:

Process proc = new Process();  proc .StartInfo.FileName = strScript;  proc .StartInfo.WorkingDirectory = strDirectory;  proc .StartInfo.CreateNoWindow = true;  proc .StartInfo.UseShellExecute = false;  proc .StartInfo.RedirectStandardOutput = true;  proc .Start();

然后設(shè)置線程連續(xù)讀取輸出的字符串:

eventOutput = new AutoResetEvent(false);  AutoResetEvent[] events = new AutoResetEvent[1];  events[0] = m_eventOutput;   m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) );  m_threadOutput.Start();  WaitHandle.WaitAll( events );

線程函數(shù)如下:

private void DisplayOutput()  {   while ( m_procScript != null && !m_procScript.HasExited )   {  string strLine = null;  while ( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null)  {   m_txtOutput.AppendText( strLine + "\r\n" );   m_txtOutputm_txtOutput.SelectionStart = m_txtOutput.Text.Length;   m_txtOutput.ScrollToCaret();  }  Thread.Sleep( 100 );   }   m_eventOutput.Set();  }

這里要注意的是,使用以下語句使TextBox顯示的總是***添加的,而AppendText而不使用+=,是因為+=會造成整個TextBox的回顯使得整個顯示區(qū)域閃爍

m_txtOutput.AppendText( strLine + "\r\n" );  m_txtOutput.SelectionStart = m_txtOutput.Text.Length;  m_txtOutput.ScrollToCaret();

“C#管道技術(shù)怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI