c# - 在两个 C# 进程之间交换消息
问题描述
我有两个用 C# 编写的控制台应用程序。我正在尝试在他们之间交换消息。为了做到这一点,我使用了非持久化内存映射文件。在我的场景中,一个控制台应用程序是父级,另一个是子级。有时,父母会向孩子发送消息。其他时候,孩子会向父母发送消息。
我无法弄清楚如何做到这一点。就像每个过程都在听或说。它并没有积极地做这两件事。此时,我正在尝试使用struct
定义为这样的两个进程之间交换消息:
public struct Message
{
public string Source { get; set; }
public string Text { get; set; }
}
我的父控制台应用程序具有如下所示的方法:
private void SendMessageToChild(string text, int childHandle)
{
Console.WriteLine("Sending message to child...");
var messageChannelFileName = childHandle.ToString() + ".msgs";
using (var messageChannelFile = MemoryMappedFile.CreateOrOpen(messageChannelFileName, 10240))
{
using (var memoryMappedAccessor = messageChannelFile.CreateViewAccessor())
{
var message = new Message();
message.Text = text;
message.Source = "Parent";
memoryMappedAccessor.Write<Message>(0, ref message);
}
}
Console.ReadKey(); // This is to keep the memory mapped file open for the child to be able to read it
Console.WriteLine("Successfully sent message to child.");
}
我的子控制台应用程序(进程)具有如下所示的方法:
private void StartListening()
{
Task.Run(() =>
{
var messageChannelFileName = Process.GetCurrentProcess().Id + ".msgs";
using (var messageChannelFile = MemoryMappedFile.OpenExisting(messageChannelFileName, MemoryMappedFileRights.Read))
{
var message = new Message();
using (var messageAccessor = messageChannelFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
{
messageAccessor.Read<Message>(0, out message);
Console.WriteLine(message.Text);
}
}
Console.ReadKey(); // This is to keep the memory mapped file
});
}
这种方法行不通。我从来没有看到打印到控制台窗口的消息。同时,我看不到来回发送消息的方法。在我看来,Console.ReadKey
双方都需要锁定文件。
我是不是误会了什么?我是否使用错误的东西在两个进程之间交换消息?我知道我不能在我的场景中使用管道,这就是我使用内存映射文件的原因。
解决方案
两个进程之间的通信真的很容易
例如父进程这样做:
// create EventWaitHandle, MemoryMapped and accessor
ewh = new EventWaitHandle(false, EventResetMode.AutoReset, "ewhFreePIE");
memory = MemoryMappedFile.CreateOrOpen("hookFreePIE", 68, MemoryMappedFileAccess.ReadWrite);
accessor = memory.CreateViewAccessor();
:
:
// Send message with accessor.write
ewh.Set();//say to other process, there is something to read
子进程示例:
memory = MemoryMappedFile.CreateOrOpen("hookFreePIE", 68, MemoryMappedFileAccess.ReadWrite);
accessor = memory.CreateViewAccessor();
ewh = EventWaitHandle.OpenExisting("ewhFreePIE");
:
:
// sample of loop
public void StartLoop()
{
while (running)
{
ewh.WaitOne();// wait Set() of another or same process
if (cmdtostop) //you could create cmdstop inside memorymapped file (set first byte to 1 for example
{
running = false;
}
else
{
//do something with data , using accessor.Read
}
}
如果你想让孩子发送给父母,你可以创建另一个 EventWaihandle 并从孩子到父母做同样的事情
进程完成时不要忘记释放资源
推荐阅读
- apache-storm - 在 Apache Storm 中,如何确定组件(spout/bolt)是否发生故障并重新启动?
- consul - 领事代理更改健康端点
- mpi - 包含带有用户定义宏的 MPI 头文件
- excel - 我有一个带有燃料下拉菜单的组合框。我需要它,以便选择燃料从一张纸上复制数据并将其粘贴到另一张纸上
- laravel - 为什么我的删除按钮返回错误 404
- javascript - 使用相同数据的文本输入中的文本更改复制组件反应js
- qt5 - QThread 总是卡在等待中
- spring - Spring Boot 应用程序中的响应代码为 404
- excel - 在 excel 数据透视过滤器中选择多个项目。但是在提及代码中的项目时,我不想具体说明。还有比这更好的方法吗?
- zabbix - 如何将 Zabbix 图表添加到您的网站?