WPF的消息类型用于交流的交流

发布于 2025-02-11 13:42:23 字数 104 浏览 0 评论 0原文

有没有办法为WPF .NET 6.0程序创建消息队列?我看到了MSMQ的文档,但似乎在.NET 6.0中不支持这一点。

我希望能够与同一程序的另一个实例进行通信。最好的方法是什么?

Is there a way to create a message queue for a WPF .NET 6.0 program? I saw documentation for MSMQ but it looks like that is not supported in .NET 6.0.

I want to be able to communicate to another instance of the same program. What would be the best way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2025-02-18 13:42:23

我发现的最好的替代方案是system.io.io.pipes中的管道。

发送/接收字符串的示例:

        private bool PipeSend(string message)
        {
            using (NamedPipeServerStream namedPipeServer
                = new NamedPipeServerStream(ConfigurationManager.AppSettings["AppGUID"]!))
            {
                // In this example I check if there is a client ready to receive the 
                // data otherwise I cancel the connection.
                CancellationTokenSource cts = new();
                namedPipeServer.WaitForConnectionAsync(cts.Token);
                Thread.Sleep(500);
                if (namedPipeServer.IsConnected)
                {
                    byte[] buffer = message.Select(c => (byte)c).ToArray();
                    namedPipeServer.Write(buffer, 0, buffer.Length);
                    return true;
                }
                else
                {
                    cts.Cancel();
                    return false;
                }
            }
        }

        private void PipeReceive()
        {
            using (NamedPipeClientStream namedPipeClient
                = new NamedPipeClientStream(ConfigurationManager.AppSettings["AppGUID"]!))
            {
                namedPipeClient.Connect();
                if (namedPipeClient.IsConnected)
                {
                    List<char> buffer = new();

                    int b = namedPipeClient.ReadByte();
                    while (b != -1)
                    {
                        buffer.Add((char)b);
                        b = namedPipeClient.ReadByte();
                    }

                    string message = new(buffer.ToArray());
                    // Do something with the message
                }
            }
        }

The best alternative I found was named pipes in System.IO.Pipes.

Example of sending/receiving a string:

        private bool PipeSend(string message)
        {
            using (NamedPipeServerStream namedPipeServer
                = new NamedPipeServerStream(ConfigurationManager.AppSettings["AppGUID"]!))
            {
                // In this example I check if there is a client ready to receive the 
                // data otherwise I cancel the connection.
                CancellationTokenSource cts = new();
                namedPipeServer.WaitForConnectionAsync(cts.Token);
                Thread.Sleep(500);
                if (namedPipeServer.IsConnected)
                {
                    byte[] buffer = message.Select(c => (byte)c).ToArray();
                    namedPipeServer.Write(buffer, 0, buffer.Length);
                    return true;
                }
                else
                {
                    cts.Cancel();
                    return false;
                }
            }
        }

        private void PipeReceive()
        {
            using (NamedPipeClientStream namedPipeClient
                = new NamedPipeClientStream(ConfigurationManager.AppSettings["AppGUID"]!))
            {
                namedPipeClient.Connect();
                if (namedPipeClient.IsConnected)
                {
                    List<char> buffer = new();

                    int b = namedPipeClient.ReadByte();
                    while (b != -1)
                    {
                        buffer.Add((char)b);
                        b = namedPipeClient.ReadByte();
                    }

                    string message = new(buffer.ToArray());
                    // Do something with the message
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文