利用Visual C#实现Window管道技术

会议室55

会议室55

2016-02-19 16:15

有了下面这个利用Visual C#实现Window管道技术教程,不懂利用Visual C#实现Window管道技术的也能装懂了,赶紧get起来装逼一下吧!

  管道技术一般采用Window API来实现,最近我试着用C#来实现Windows管道技术,发现C#本身方便的进程线程机制使工作变得简单至极,随手记录一下,推荐给大家。

  首先,我们可以通过设置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(); 

  然后设置线程连续读取输出的字符串:

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  

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

  线程函数如下:

  

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

  这里要注意的是,使用以下语句使TextBox显示的总是最新添加的,而AppendText而不使用+=,是因为+=会造成整个TextBox的回显使得整个显示区域闪烁

  

m_txtOutput.AppendText( strLine + "rn" );m_txtOutput.SelectionStart = m_txtOutput.Text.Length;m_txtOutput.ScrollToCaret(); 

  为了不阻塞主线程,可以将整个过程放到一个另一个线程里就可以了

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

利用Visual C#实现Window管道技术

编程语言 网络编程
利用Visual C#实现Window管道技术

C#实现Window管道技术

电脑网络
C#实现Window管道技术

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

利用Visual C#实现ICMP网络协议

编程语言 网络编程
利用Visual C#实现ICMP网络协议

利用Visual C#实现Reversi游戏开发

编程语言 网络编程
利用Visual C#实现Reversi游戏开发

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

利用Visual C#实现任务栏通知窗口

编程语言 网络编程
利用Visual C#实现任务栏通知窗口

Visual C#实现Windows信使服务

编程语言 网络编程
Visual C#实现Windows信使服务

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

mcafee是什么

mcafee是什么

通过JavaScript以及ActiveX控件获得客户端的机器名

通过JavaScript以及ActiveX控件获得客户端的机器名
下拉加载更多内容 ↓