C# 两个进程之间通讯(管道通信 )
#region 客户端
NamedPipeClientStream pipeClient =
new NamedPipeClientStream("localhost", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
StreamWriter sw = null;
private void Form1_Load(object sender, EventArgs e)
{
try
{
pipeClient.Connect(5000);
sw = new StreamWriter(pipeClient);
sw.AutoFlush = true;
}
catch (Exception ex)
{
MessageBox.Show("连接建立失败,请确保服务端程序已经被打开。");
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (sw != null)
{
sw.WriteLine(this.richTextBox1.Text);
}
else
{
MessageBox.Show("未建立连接,不能发送消息。");
}
}
#endregion
#region 服务端
NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
pipeServer.BeginWaitForConnection((o) =>
{
NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
pServer.EndWaitForConnection(o);
StreamReader sr = new StreamReader(pServer);
while (true)
{
this.Invoke((MethodInvoker)delegate { listView1.Items.Add(sr.ReadLine()); });
}
}, pipeServer);
});
}
#endregion
最新文章
- iOS网络推送消息
- 手动部署servlet
- You and Your Research(Chinese)
- Java系列:Add Microsoft SQL JDBC driver to Maven
- cocos2d ccmenulabel
- saltstack之(十一)扩展组件salt-returners
- 161025、java提高篇之关键字static
- Cocos2D-x权威指南:通过节点控制屏幕中的全体渲染对象
- C#中的DateTime:本周第一天,本月第一天,今年第一天,本周第一天的时间
- UML建模之状态图(Statechart Diagram)
- CSS样式----CSS的继承性和层叠性(图文详解)
- DSAPI 生成桌面图标(带数字)
- 从壹开始前后端分离 [.netCore 填坑 ] 三十三║ ⅖ 种方法实现完美跨域
- 2018-2019-2 网络对抗技术 20165225 Exp6 信息搜集与漏洞扫描
- 【转】Mybatis源码解读-设计模式总结
- 点击导出table表格
- db2 OLAP函数使用
- 51nod 1069 Nim游戏 + BZOJ 1022: [SHOI2008]小约翰的游戏John(Nim游戏和Anti-Nim游戏)
- Java判断对象是否为NULL
- HTML - 网页特殊字符大全(转)
热门文章
- [python] super() 用法
- 集合运算 - Java实现集合的交、并、差
- 为什么 ConcurrentHashMap 的读操作不需要加锁?
- C++学习 之 类中的特殊函数和this指针(笔记)
- js文件的框架
- softmax函数笔记
- 深入理解hive基础学习
- SSM处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
- python cv2读取rtsp实时码流按时生成连续视频文件
- Windows访问VirtualBox的Redis服务器