C#实现Window管道技术

2016-01-29 12:42 156 1 收藏

C#实现Window管道技术,C#实现Window管道技术

【 tulaoshi.com - ASP.NET 】


之前发了一篇使用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 + "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://www.tulaoshi.com/n/20160129/1487278.html

延伸阅读
在.Net 中 DataGrid 虽然有排序的功能,但并不支持双向的排序。用到了,看了些相关的帖子,自己尝试了一种方法,竟然也行得通,主要是用DataGrid.Attributes 存了一个参数,同时在onSortCommand中修改了DataGridColumn的SortExpression. 代码如下: private void BindData(){ DataTable dt = .......; if(dt != null) { DataVi...
现在有很多网络管理软件都具备网络上信息实时传送的功能,虽然有些网络通讯软件功能比较强大,有的软件不仅可以传送文本信息,还可以传送二进制文件等。但它们都有一个无法克服的缺点,那就是分发比较困难,信息传送双方计算机都需要安装通讯软件的客户端和服务器端软件,并且只有但双方都打开相应软件时,才可能进行信息传送。而信使通讯...
最近经朋友介绍开始玩 密传 网络游戏 升级升级,突然觉得太费键盘,于是自己用C#写了一个程序,想代替我的操作,自己去打怪物,自己升级 用这个东西升了好多级了,现在把源码贴出来,和大家共享,欢迎大家批评指正,感激不尽。 程序大概分成两个部分,一个部分是类库,一个是应用程序 大概的思路就是找到游戏进程的主窗口句柄,然后发送游戏...
橡皮区矩形 CRectTracker C# 实现 作者:fanjunxing 下载源代码 本文要求读者熟悉 C# 开发环境: Visual Studio .NET 2003 Windows 2000 测试环境:Windows 2000 更新记录:2004.4.7 第一次更新 使用许可:代码...
本文给出一个用 C# 编程实现 读写 Binary 的实例代码,对于初学者来说是个不可多得的参考性文章…… 以下是引用片段: //返回blob数据 public MemoryStream getBlob(string SQL) ...{ try ...{ Db_Conn(); cmd = new OleDbCommand(SQL, Conn); cmd.Comma...

经验教程

741

收藏

25

精华推荐

c# SQLHelper(for winForm)实现代码

c# SQLHelper(for winForm)实现代码

大清第一美

CRectTracker C# 实现橡皮区矩形

CRectTracker C# 实现橡皮区矩形

GA6115

Windows管道技术简述

Windows管道技术简述

微点播萍子

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部