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

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

此Form1为Client的窗c#教程体,如下图所示。

在这里插入图片描述

后端代码,如下。

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的窗体,如下图所示
在这里插入图片描述

后端代码,如下。

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进程通信内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值