当前位置: 首页 > news >正文

C# 中实现不同程序进程间消息交互

使用管道Pipe方式实现:限于同一台主机下不同程序之间的数据交互

服务端实现:

private void StartPipe()
{
    var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 5, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
    ThreadPool.QueueUserWorkItem(delegate
    {
        pipeServer.BeginWaitForConnection(async (o) =>
        {
            NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
            pServer.EndWaitForConnection(o);
            StreamReader sr = new StreamReader(pServer);
            while (true)
            {
                string message = await sr.ReadLineAsync();
                if (message != null)
                {
                    this.Invoke(new Action(() => GetClientMessage(message)));
                    //(MethodInvoker)delegate { selectPuller.SelectedValue = new SelectItem(message); };
                    this.Invoke((MethodInvoker)delegate
                    {
                        selectPuller.SelectedValue = new SelectItem(message);
                    });
                }
            }
        }, pipeServer);
    });
}


private void GetClientMessage(string message)
{
    Console.WriteLine($"收到Client管道信息:{message}");
}

客户端:

 static NamedPipeClientStream pipeClient =
 new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
 StreamWriter sw = null;
 //连接服务端
 private void ConnectPipe()
 {
     try
     {
         pipeClient.Connect(5000);
         sw = new StreamWriter(pipeClient);
         sw.AutoFlush = true;
     }
     catch (Exception ex)
     {
         MessageBox.Show("连接建立失败,请确保服务端程序已经被打开。");
         this.Close();
     }
 }

//消息发送,在winfrom页面中输入
private void btnPipe_Click(object sender, EventArgs e)
{
    if (sw != null && pipeClient.IsConnected)
    {
        if (!pipeClient.IsConnected) {
            pipeClient.Connect(5000);
            sw = new StreamWriter(pipeClient);
            sw.AutoFlush = true;
        }
        sw.WriteLine(this.txtPipeMsg.Text);
    }
    else
    {
        MessageBox.Show("未建立连接,不能发送消息。");
    }
}

http://www.dtcms.com/a/106470.html

相关文章:

  • 【Linux网络#18】:深入理解select多路转接:传统I/O复用的基石
  • ETCD --- lock详解
  • JAVASE(十五)正则表达式
  • 2024年最新版零基础详细Java知识笔记【反射】⑩
  • Python实现 MCP 客户端调用(高德地图 MCP 服务)查询天气工具示例
  • Linux系统
  • Oracle 23ai Vector Search 系列之3 集成嵌入生成模型(Embedding Model)到数据库示例,以及常见错误
  • 16变量命名风格
  • windows部署docker
  • electron-update + nginx热更新
  • 【深度学习:进阶篇】--2.1.多分类与TensorFlow
  • 2025 年山东危化品经营单位考试攻略分享​
  • 二分查找与二叉树中序遍历——面试算法
  • OpenCV单窗口显示多图片
  • MySQL分组的时候遇到ONLY_FULL_GROUP_BY报错和解决
  • html+css+javaScript实现一个扫雷游戏
  • MATLAB 代码学习
  • Spring Boot 3.0 + JDK 17整合SpringDoc实战指南
  • 清明假期在即
  • 5G网络中SIB System Information Blocks系统信息块
  • 安美数字酒店宽带运营系统存在SQL注入漏洞
  • 云端商标管理系统如何确保用户数据安全?
  • 《永动之城的舞者》
  • Spring Boot 集成 Redis 连续操作键值对示例
  • MySQL性能:存储过程+触发器基础实战攻略
  • Mysql中,利用窗口函数来优化掉子查询或者group by
  • IPD推进中关键角色与岗位(二)系统工程师SE确保产品开发过程的高效协同,减少项目失败的风险
  • mysql数据库通过命令行导入sql文件
  • Rust 中的高效视频处理:利用硬件加速应对高分辨率视频
  • 【2025】实操成功-使用Python连接谷歌邮箱发送邮件