国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Visual C#中實現Window管道技術

2023-05-20 20:23:39
字體:
來源:轉載
供稿:網友

管道技術一般采用Window API來實現,在C#中實現管道技術其實也很簡單,因為C#中有方便的進程和線程機制。

(1)通過設置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();

(2)設置線程連續讀取輸出的字符串:

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 );

(3)完整線程函數:

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_txtOutput.SelectionStart = m_txtOutput.Text.Length;
           m_txtOutput.ScrollToCaret();
          }
          Thread.Sleep( 100 );
         }
         m_eventOutput.Set();
        }

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

m_txtOutput.AppendText( strLine + "/r/n" );

m_txtOutput.SelectionStart = m_txtOutput.Text.Length;

m_txtOutput.ScrollToCaret();

為了不阻塞主線程,可以將整個過程放到一個另一個線程里。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 民县| 雅安市| 青铜峡市| 芜湖市| 横山县| 洛川县| 灌南县| 寻乌县| 新绛县| 浪卡子县| 榆中县| 德兴市| 民和| 黎川县| 苍山县| 芮城县| 咸丰县| 新绛县| 凉山| 循化| 阿巴嘎旗| 济源市| 那坡县| 康保县| 达尔| 威远县| 祁阳县| 加查县| 钟祥市| 永昌县| 米易县| 万荣县| 秀山| 伊吾县| 潞西市| 恩施市| 广德县| 江口县| 瑞金市| 鹤峰县|