当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C#实现Window管道技术

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 C#实现Window管道技术


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 82 ::
收藏到网摘: n/a


之前发了一篇使用Window API来实现管道技术的文章,后来改用C#来实现相同的效果,发现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();
然后设置线程连续读取输出的字符串:
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 + "\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(); 为了不阻塞主线程,可以将整个过程放到一个另一个线程里就可以了