【问题标题】:Send a message to already running WPF process [duplicate]向已运行的 WPF 进程发送消息 [重复]
【发布时间】:2019-09-08 22:47:39
【问题描述】:

我在 C# 中有一个 WPF 和 winform 应用程序。我使用

从 winform 应用程序调用 wpf 应用程序
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\wpfapp.exe";
startInfo.Arguments = data; //string result data from webservice;
Process.Start(startInfo);

没关系,我可以使用作为参数发送的参数从 winform 运行 wpf ui。但现在我有一个问题。现在我想在正在运行的 wpf 窗口中更新消息。

wpf 窗口已经运行并显示消息。稍后我想向同一个 wpf 窗口发送另一条消息。 我们如何做到这一点?

if (ProgramIsRunning(exepath))
{
    // here we need to add the code to send message to the same wpf window.
}
else
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\wpfapp.exe";
    startInfo.Arguments = data; // string result data from webservice;
    Process.Start(startInfo);
}

请帮忙。

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

如果您不喜欢 post 中描述的管道,您可以在 WPF 应用程序中使用 Nancy 托管简单的 REST 服务器,并使用 HttpClientRestSharp 之类的东西与之通信。

消息监听器应用示例:

class Program {
    static void Main(string[] args) {
        var baseUri = new Uri("http://localhost:1234");
        var nancyHost = new NancyHost(baseUri);
        nancyHost.Start();

        Thread.Sleep(-1);
    }
}
public class SimpleRestModule : NancyModule {
    public SimpleRestModule() {
        Get["/Message"] = (args) => {
            return "Hello from server";
        };

        Post["/Message"] = (args) => {
            var receivedMessage = Request.Body.AsString();
            return "Message received";
        };
    }
}

示例消息发送应用程序:

class Program {
    static void Main(string[] args) {
        var httpClient = new HttpClient();

        var response = httpClient.PostAsync("http://localhost:1234/Message", new StringContent("Test message")).Result;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    相关资源
    最近更新 更多