服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#使用命名管道Pipe进行进程通信实例详解

C#使用命名管道Pipe进行进程通信实例详解

2022-10-25 13:18樱花花 C#

这篇文章主要介绍了C#使用命名管道Pipe进行进程通信实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.新建解决方案NamedPipeExample 新建两个项目:Client和Server,两者的输出类型均为“Windows 应用程序”。整个程序的结构如下图所示。

C#使用命名管道Pipe进行进程通信实例详解

此Form1为Client的窗体,如下图所示。

C#使用命名管道Pipe进行进程通信实例详解

后端代码,如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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("连接建立失败,请确保服务端程序已经被打开。");
        this.Close();
      }
    }
 
    private void btnSend_Click(object sender, EventArgs e)
    {
      if (sw != null)
      {
        sw.WriteLine(this.txtMessage.Text);
      }
      else
      {
        MessageBox.Show("未建立连接,不能发送消息。");
      }
    }
  }
}

此Form1为Server的窗体,如下图所示

C#使用命名管道Pipe进行进程通信实例详解

后端代码,如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
    {
 
    }
  }
}

先运行Server再运行Client

到此这篇关于C#使用命名管道Pipe进行进程通信实例详解的文章就介绍到这了,更多相关C# Pipe进程通信内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_37192571/article/details/108865099

延伸 · 阅读

精彩推荐
  • C#C#中yield用法使用说明

    C#中yield用法使用说明

    本文介绍了C#中yield的使用方法,yield 语句不能出现在匿名方法,其他相关内容就仔细阅读下文吧...

    C#教程网8832021-10-29
  • C#C#学习笔记- 浅谈数组复制,排序,取段,元组

    C#学习笔记- 浅谈数组复制,排序,取段,元组

    下面小编就为大家带来一篇C#学习笔记- 浅谈数组复制,排序,取段,元组。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看...

    C#教程网4612021-12-03
  • C#C#操作SQLite数据库之读写数据库的方法

    C#操作SQLite数据库之读写数据库的方法

    这篇文章主要介绍了C#操作SQLite数据库之读写数据库的方法,简单分析了C#针对SQLite数据库的读写及显示等操作相关技巧,具有一定参考借鉴价值,需要的朋友可...

    zhenyongyuan12312452021-11-30
  • C#利用C#版OpenCV实现圆心求取实例代码

    利用C#版OpenCV实现圆心求取实例代码

    这篇文章主要给大家介绍了关于如何利用C#版OpenCV实现圆心求取的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    小y11162022-07-21
  • C#当用户退出点击浏览器后退仍可回到原来页面的解决方案

    当用户退出点击浏览器后退仍可回到原来页面的解决方案

    这篇文章主要介绍了当用户退出点击浏览器后退仍可回到原来页面的解决方案 的相关资料,需要的朋友可以参考下...

    jerrylsxu3492021-11-15
  • C#C#实现带消息数的App图标

    C#实现带消息数的App图标

    这篇文章主要介绍了如何使用C#实现带消息数的App图标的方法,并附上全部源码,分享给大家,有需要的小伙伴可以参考下。...

    C#教程网7222021-11-05
  • C#WPF自定义实现IP地址输入控件

    WPF自定义实现IP地址输入控件

    这篇文章主要给大家介绍了关于WPF自定义实现IP地址输入控件的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WPF具有一定的参考学习...

    一叶知秋,知寒冬7872022-07-24
  • C#实例代码讲解c# 线程(上)

    实例代码讲解c# 线程(上)

    这篇文章主要介绍了讲解c# 线程的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...

    HueiFeng3822022-09-20