• C# 命名管道


    命名管道作用:方便程序跨进程通讯;

    使用pipeList工具可查询系统中所有命名管道

    https://docs.microsoft.com/zh-cn/sysinternals/downloads/pipelist

    C#实现代码如下:

        public partial class Form1 : Form
        {
            // 命名管道客户端
            NamedPipeClientStream pipeClient = null;
            StreamWriter swClient = null;
            StreamReader srClient = null;
    
            public Form1()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false;
            }
    
            // 创建命名管道
            private void button1_Click(object sender, EventArgs e)
            {
                backgroundWorker1.RunWorkerAsync();
                txtInfo.AppendText("创建命名管道" + Environment.NewLine);
            }
    
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("testPipe", PipeDirection.InOut))
                {
                    pipeServer.WaitForConnection();
    
                    var data = new byte[10240];
                    var count = pipeServer.Read(data, 0, 10240);
                    StreamReader sr = new StreamReader(pipeServer);
                    using (StreamWriter sw = new StreamWriter(pipeServer))
                    {
                        sw.AutoFlush = true;
                        sw.WriteLine("hello " + DateTime.Now.ToString());
                        while (true)
                        {
                            string str = sr.ReadLine();
                            File.AppendAllText(Application.StartupPath + "//log.txt", DateTime.Now.ToLocalTime().ToString() + " " + str + Environment.NewLine);
                            txtInfo.AppendText(str + Environment.NewLine);
                            sw.WriteLine("send to client " + DateTime.Now.ToString());
                            Thread.Sleep(1000);
                        }
                    }
                }
            }
    
            // 连接命名管道
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    pipeClient = new NamedPipeClientStream("localhost", "testPipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
                    pipeClient.Connect(5000);
                    swClient = new StreamWriter(pipeClient);
                    srClient = new StreamReader(pipeClient);
                    swClient.AutoFlush = true;
                    backgroundWorker2.RunWorkerAsync();
    
                    txtInfo.AppendText("连接命名管道" + Environment.NewLine);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("连接建立失败,请确保服务端程序已经被打开。" + ex.ToString());
                }
            }
    
            // 发送消息
            private void button3_Click(object sender, EventArgs e)
            {
                if (swClient != null)
                {
                    swClient.WriteLine(this.textBox1.Text);
                }
                else
                {
                    MessageBox.Show("未建立连接,不能发送消息。");
                }
            }
    
            // 接收消息
            private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
            {
                while (true)
                {
                    if (srClient != null)
                    {
                        txtInfo.AppendText(srClient.ReadLine() + System.Environment.NewLine);
                    }
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // button1.PerformClick();
            }
        }
  • 相关阅读:
    Qt中的角度转弧度
    Qt5鼠标事件及实例
    POJ 2239 Selecting Courses【最大匹配】
    POJ 1325 Machine Schedule【最小点覆盖】
    POJ 1469 COURSES【二分图最大匹配】
    POJ 1274 The Perfect Stall【二分图最大匹配】
    poj2226Muddy Fields【最小点覆盖(建图的思路比较好)】
    hdu4160Dolls【最小路径覆盖】
    hdu2444The Accomodation of Students【判断二分图+最大匹配】
    HLG1407Leyni的游戏【最小点权覆盖集】
  • 原文地址:https://www.cnblogs.com/zjfree/p/11265242.html
Copyright © 2020-2023  润新知