C#はプロセス通信の例に名前付きパイプパイプを使用します

1.新しいソリューションNamedPipeExample

  1. クライアントとサーバーの2つの新しいプロジェクトを作成します。どちらの出力タイプも「Windowsアプリケーション」です。プログラム全体の構成を下図に示します。
    ここに画像の説明を挿入
    このForm1は、次の図に示すように、クライアントのフォームです。
    にとって
    バックエンドコードは次のとおりです。
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はサーバーフォームであり、次の図に示すように、
ここに画像の説明を挿入
バックエンドコードは次のとおりです。

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

        }
    }
}

最初にサーバーを実行し、次にクライアントを実行します

おすすめ

転載: blog.csdn.net/qq_37192571/article/details/108865099