C#使用命名管道Pipe進(jìn)行進(jìn)程通信實(shí)例詳解
1.新建解決方案NamedPipeExample 新建兩個項(xiàng)目:Client和Server,兩者的輸出類型均為“Windows 應(yīng)用程序”。整個程序的結(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("連接建立失敗,請確保服務(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity幸運(yùn)轉(zhuǎn)盤實(shí)戰(zhàn)項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Unity幸運(yùn)轉(zhuǎn)盤實(shí)戰(zhàn)項(xiàng)目,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法
C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法,需要的朋友可以參考一下2013-03-03
C#使用泛型隊(duì)列Queue實(shí)現(xiàn)生產(chǎn)消費(fèi)模式
這篇文章介紹了C#使用泛型隊(duì)列Queue實(shí)現(xiàn)生產(chǎn)消費(fèi)模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
C#實(shí)現(xiàn)Excel轉(zhuǎn)PDF時設(shè)置內(nèi)容適應(yīng)頁面寬度
將Excel轉(zhuǎn)為PDF格式時,通常情況下轉(zhuǎn)換出來的PDF頁面都是默認(rèn)的寬度大小。所以本文提供了C#實(shí)現(xiàn)Excel轉(zhuǎn)PDF時設(shè)置內(nèi)容適應(yīng)頁面寬度的示例代碼,需要的可以參考一下2022-04-04
C#中遍歷DataSet數(shù)據(jù)集對象實(shí)例
這篇文章主要介紹了C#中遍歷DataSet數(shù)據(jù)集對象實(shí)例,經(jīng)常忘記如何操作DataSet,這里記下來并分享,讓需要的朋友可以參考下2014-08-08
字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例
下面小編就為大家分享一篇字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

