开发工具:
文件大小: 128kb
下载次数: 0
上传时间: 2012-11-30
详细说明: 命名管道的实际用例。 // 服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO.Pipes; using System.IO; namespace CSharpTraceServer { public partial class CSharpTraceForm : Form { NamedPipeServerStream server; StreamReader reader; public CSharpTraceForm() { InitializeComponent(); } private void CSharpTraceForm_Load(object sender, Event Args e) { // 命名管道 server = new NamedPipeServerStream("_"); // 后台线程 backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork); backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); backgroundWorker.RunWorkerAsync(); // 窗口事件关联 this.SizeChanged += new EventHandler(CSharpTraceForm_SizeChanged); // 窗口配置 WindowState = FormWindowState.Maximized; } private void AdjustSize() { int X = 0; int Y = menuStrip1.Size.Height; int Width = Size.Width; int Height = Size.Height; textboxInformation.Location = new Point(X, Y); textboxInformation.Size = new Size(Width, Height); } void CSharpTraceForm_SizeChanged(object sender, EventArgs e) { AdjustSize(); } private void clearToolStripMenuItem_Click(object sender, EventArgs e) { textboxInformation.Text = ""; } void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { string strInformation; while (true) { if (!server.IsConnected) { server.Close(); server = new NamedPipeServerStream("CSharpTrace"); reader = new StreamReader(server); server.WaitForConnection(); } strInformation = reader.ReadLine(); backgroundWorker.ReportProgress(1, strInformation); } } void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { textboxInformation.Text += e.UserState + "\r\n"; } } } // 客户端 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Pipes; using System.Threading; using System.IO; namespace CSharpTraceDll { public class CSharpTraceDll { // 简易接口 public static void Debug(string strInformation) { Init(); WriteLine(strInformation); } // 初始化连接环境 public static void Init() { // 初始化命名管道 if (_client == null) { _client = new NamedPipeClientStream("_"); } // 启动线程 if (_thread == null) { _thread = new Thread(new ThreadStart(ThreadRun)); _thread.IsBackground = true; _thread.Start(); } } // 断开连接 public static void UnInit() { // 终止线程 if (_thread != null) { _thread.Abort(); _thread = null; } // 释放命名管道 if (_client != null) { _client.Close(); _client = null; } } // 发送数据 public static void WriteLine(string strInformation) { // 检查合法性 if (_client == null) return; if (_writer == null) return; try { // 检查连接状态 if (_client.IsConnected) { // 发送信息 string strLine = string.Format("[{0}]:{1}", System.DateTime.Now.ToString(), strInformation); _writer.WriteLine(strLine); } } catch (Exception e) { // 捕获连接异常 Console.WriteLine(e.Message); } } // 内部线程 private static void ThreadRun() { while (true) { // 确保连接状态 if (!_client.IsConnected) { _client.Close(); _client = new NamedPipeClientStream(".", "CSharpTrace"); _writer = new StreamWriter(_client); _client.Connect(); _writer.AutoFlush = true; } Thread.Sleep(1000); } } private static NamedPipeClientStream _client; private static StreamWriter _writer; private static Thread _thread; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.