簡體   English   中英

在兩個C#進程之間交換消息

[英]Exchanging messages between two C# processes

我有兩個用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來鎖定文件。

我誤會了嗎? 我使用錯誤的東西在兩個進程之間交換消息嗎? 我知道我不能在我的場景中使用管道 ,這就是為什么我要使用內存映射文件。

在2個流程之間進行交流非常容易

例如,父進程這樣做:

        // 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
        }
    }

如果您希望孩子將其發送給父母,則可以創建另一個EventWaithandle並從孩子到父母進行相同的操作

完成流程后不要忘記配置資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM