C#使用命名管道Pipe進(jìn)行進(jìn)程通信實(shí)例詳解
1.新建解決方案NamedPipeExample 新建兩個(gè)項(xiàng)目:Client和Server,兩者的輸出類型均為“Windows 應(yīng)用程序”。整個(gè)程序的結(jié)構(gòu)如下圖所示。

此Form1為Client的窗體,如下圖所示。

后端代碼,如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
namespace Client
{
public partial class Form1 : Form
{
NamedPipeClientStream pipeClient =
new NamedPipeClientStream("localhost", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
StreamWriter sw = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
pipeClient.Connect(5000);
sw = new StreamWriter(pipeClient);
sw.AutoFlush = true;
}
catch (Exception ex)
{
MessageBox.Show("連接建立失敗,請(qǐng)確保服務(wù)端程序已經(jīng)被打開。");
this.Close();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (sw != null)
{
sw.WriteLine(this.txtMessage.Text);
}
else
{
MessageBox.Show("未建立連接,不能發(fā)送消息。");
}
}
}
}
此Form1為Server的窗體,如下圖所示

后端代碼,如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Server
{
public partial class Form1 : Form
{
NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
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 { lsvMessage.Text = sr.ReadLine(); });
}
}, pipeServer);
});
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
}
}
先運(yùn)行Server再運(yùn)行Client
到此這篇關(guān)于C#使用命名管道Pipe進(jìn)行進(jìn)程通信實(shí)例詳解的文章就介紹到這了,更多相關(guān)C# Pipe進(jìn)程通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VS2015為console.readkey添加代碼片段的方法
這篇文章主要介紹了VS2015為console.readkey添加代碼片段的方法,需要的朋友可以參考下2016-12-12
C#使用doggleReport生成pdf報(bào)表的方法
這篇文章主要介紹了C#使用doggleReport生成pdf報(bào)表的方法,結(jié)合實(shí)例形式分析了C# doggleReport安裝及使用具體操作技巧,需要的朋友可以參考下2017-06-06
C#實(shí)現(xiàn)順序隊(duì)列和鏈隊(duì)列的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于C#實(shí)現(xiàn)順序隊(duì)列和鏈隊(duì)列的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
C#結(jié)合JS修改解決KindEditor彈出層問題
KindEditor 是一款出色的富文本HTML在線編輯器,這里我們講述在使用中遇到的一個(gè)問題,在部署到某些 WEB 應(yīng)用項(xiàng)目中,點(diǎn)擊類似彈出層功能時(shí),只顯示了遮罩層,而內(nèi)容層則定位無法正確顯示,所以本文給大家介紹了C#結(jié)合JS 修改解決 KindEditor 彈出層問題2024-06-06
C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法
這篇文章主要介紹了C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法,涉及C#操作時(shí)間的相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#利用Task實(shí)現(xiàn)任務(wù)超時(shí)多任務(wù)一起執(zhí)行的方法
這篇文章主要給大家介紹了關(guān)于C#利用Task實(shí)現(xiàn)任務(wù)超時(shí),多任務(wù)一起執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。2017-12-12

