温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在C#利用命名管道Pipe实现进程通信

发布时间:2020-12-29 14:06:07 来源:亿速云 阅读:484 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在C#利用命名管道Pipe实现进程通信,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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

怎么在C#利用命名管道Pipe实现进程通信

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

怎么在C#利用命名管道Pipe实现进程通信

后端代码,如下。

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实现进程通信

后端代码,如下。

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)
    {

    }
  }
}

关于怎么在C#利用命名管道Pipe实现进程通信就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI