将参数发送到驻留在另一个进程中的应用程序实例

发布于 2024-09-15 04:55:08 字数 423 浏览 2 评论 0原文

我有一个单实例应用程序(c#、WPF、.net3.51)。检查应用程序是否已实例化是通过互斥体完成的。如果应用程序已在运行,我会从已打开的应用程序实例中打开一个新窗口。到目前为止效果很好。

但是,由于应用程序扩展,我现在必须将 e.Args(或至少是它的第一个字符串)发送到驻留在另一个进程中的已运行实例。如何最好地做到这一点?

其他信息
目前,我使用全局注册的窗口消息,通过 PostMessage (HWND_BROADCAST) 发送到所有打开的应用程序。我的应用程序会查找此消息,如果收到此消息,则会打开一个新窗口。一个想法是设置 PostMessage 的参数。然而我发现了很多关于这个话题的令人困惑的信息,所以我没有勇气走这条路。除此之外,我考虑通过另一个激活逻辑替换全局 PostMessage 调用,因为全局调用似乎有一些令人讨厌的副作用。

I have a single-instance app (c#, WPF, .net3.51). The check if the app is already instantiated is done via a Mutex. If the app is already running, I open a new window from within the already opened app-instance. This works fine so far.

However due to an app extension, I now must send the e.Args (or at least the first string of it) to the already running instance which resides in another process. How is this best done?

Additional Information
Currently I use a globaly registered Window-message that I send to all open apps via PostMessage (HWND_BROADCAST). My app looks for this message and opens a new window, if this message is received. An Idea would be to set a param of PostMessage. However I found a lot of bewildering information on this topic, therefore I had not the courage to go this way. Besides of that I thought of replacing the global PostMessage-call through another activation logic, since the global call seems to have some unlovely side-effects.

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

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

发布评论

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

评论(1

无风消散 2024-09-22 04:55:08

您可以使用命名管道,它已添加到 .NET 3.5 中的 BCL 中。

在已运行的实例中创建命名管道服务器(参数接收方),并在重复应用程序中创建命名管道客户端(参数发送方)。然后,将参数从客户端发送到服务器。

如果需要,可以在 C/C++ 中创建命名管道的任一端。请参阅 Win32 CreateNamedPipe 功能。

下面是一个简单的示例,其中客户端和服务器都在单个程序中运行(下面的“FD1AF2B4...”GUID 只是一个唯一标识符,以避免与系统上已存在的命名管道发生冲突)。

class Program
{
    static void Main(string[] args)
    {
        Thread writerThread = new Thread(new ThreadStart(WriterThread));
        writerThread.Start();

        Thread readerThread = new Thread(new ThreadStart(ReaderThread));
        readerThread.Start();
    }

    static void ReaderThread()
    {
        NamedPipeServerStream server = new NamedPipeServerStream("FD1AF2B4-575A-46E0-8DF5-8AB368CF6645");
        server.WaitForConnection();

        using (var reader = new BinaryReader(server))
        {
            string arguments = reader.ReadString();
            Console.WriteLine("Received: {0}", arguments);
        }
    }

    static void WriterThread()
    {
        NamedPipeClientStream client = new NamedPipeClientStream("FD1AF2B4-575A-46E0-8DF5-8AB368CF6645");
        client.Connect(Timeout.Infinite);

        using (var writer = new BinaryWriter(client))
        {
            writer.Write("/foo /bar:33 /baz:quux");
        }
    }
}

You could use named pipes, which were added to the BCL in .NET 3.5.

Create the named pipe server (argument receiver) in the already-running instance and create the named pipe client (argument sender) in the duplicate app. Then, send the arguments from the client to server.

Either end of the named pipe can be created in C/C++ if needed. See the Win32 CreateNamedPipe function.

Below is a simple example with the client and server both running in a single program (the "FD1AF2B4..." GUID below is just a unique identifier to avoid colliding with already-existing named pipes on the system).

class Program
{
    static void Main(string[] args)
    {
        Thread writerThread = new Thread(new ThreadStart(WriterThread));
        writerThread.Start();

        Thread readerThread = new Thread(new ThreadStart(ReaderThread));
        readerThread.Start();
    }

    static void ReaderThread()
    {
        NamedPipeServerStream server = new NamedPipeServerStream("FD1AF2B4-575A-46E0-8DF5-8AB368CF6645");
        server.WaitForConnection();

        using (var reader = new BinaryReader(server))
        {
            string arguments = reader.ReadString();
            Console.WriteLine("Received: {0}", arguments);
        }
    }

    static void WriterThread()
    {
        NamedPipeClientStream client = new NamedPipeClientStream("FD1AF2B4-575A-46E0-8DF5-8AB368CF6645");
        client.Connect(Timeout.Infinite);

        using (var writer = new BinaryWriter(client))
        {
            writer.Write("/foo /bar:33 /baz:quux");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文