发送参数的应用程序实例驻留在另一个进程 [英] Sending arguments to an app-instance that resides in another process
问题描述
我有一个单实例的应用程序(C#,WPF,.net3.51)。该检查,如果该应用程序已经通过实例化一个互斥完成。如果应用程序已经在运行,笔者从已打开的应用程序实例中打开一个新窗口。这工作得很好至今。
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.
然而,由于一个应用程序扩展,我现在必须发送e.Args(或至少它的第一串),以驻留在另一个过程已经运行的实例。这是怎么做最好?
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?
其他信息
目前我使用一个globaly注册窗口消息我发送给所有打开的应用程序通过PostMessage的(HWND_BROADCAST)说。我的应用程序查找此消息,并打开一个新的窗口,如果收到此消息。一个想法是设定PostMessage的一个参数。不过,我发现了很多令人困惑的信息,关于这个主题,所以我没有胆量走这条路。再说了,我认为通过另一个激活逻辑取代了全球PostMessage的调用,因为全球呼吁似乎有一些不可爱的副作用。
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.
推荐答案
您可以使用命名管道,它被添加到BCL在.NET 3.5。
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.
无论是命名管道的末端可以在C / C如果需要创建++。请参阅Win32 <一href="http://msdn.microsoft.com/en-us/library/aa365150%28VS.85%29.aspx"><$c$c>CreateNamedPipe$c$c>功能。
Either end of the named pipe can be created in C/C++ if needed. See the Win32 CreateNamedPipe
function.
下面是一个简单的例子与在客户端和服务器都在一个单一的节目运行(以下简称FD1AF2B4 ...GUID下面仅仅是一个唯一的标识符,以避免与系统上已存在的命名管道碰撞)。
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");
}
}
}
这篇关于发送参数的应用程序实例驻留在另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!