C# 利用 命名管道 进行 进程间通讯
最近在做一个数据库同步软件.!!
程序 服务端为 一个winform + windows Service 二大模块.!
由于程序功能的需求. 需要winform 与windows Service进程通讯. 因此使用了 命名管道 来实现功能需求.!
以此记下笔记 , 并付上一Demo
有关 NamedPipeServerStream 类 官方MSDN文档说明
NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
pipeServer.BeginWaitForConnection((o) =>;
{
NamedPipeServerStream server = (NamedPipeServerStream)o.AsyncState;
server.EndWaitForConnection(o);
StreamReader sr = new StreamReader(server);
StreamWriter sw = new StreamWriter(server);
string result = null;
string clientName = server.GetImpersonationUserName();
while (true)
{
result = sr.ReadLine();
if (result == null || result == "bye")
break;
this.Invoke((MethodInvoker)delegate { lsbMsg.Items.Add(clientName+" : "+result); });
}
}, pipeServer);
});
}
有关 NamedPipeClientStream 类 官方MSDN文档说明
View Code
1 NamedPipeClientStream pipeClient = new NamedPipeClientStream("192.168.1.100", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None); StreamWriter sw = null;
2 void Form2_Load(object sender, EventArgs e)
3 {
4 pipeClient.Connect();
5 sw = new StreamWriter(pipeClient);
6 sw.AutoFlush = true;
7 }
8 private void button1_Click_1(object sender, EventArgs e)
9 {
10 sw.WriteLine(textBox1.Text);
11 }
如须转载请说明出处
多抽出一分钟时间学习,让你的生命更加精彩,敢想敢做,努力做最好!
博客园:
JonneyDong 地址:http://www.cnblogs.com/jonneydong/