C#:如何 - 接受新参数的单实例应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/917883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# : how to - single instance application that accepts new parameters?
提问by Led
I'm creating a (C#) program that downloads binaries using NZB files, there may only be one instance of my application running at any time.
我正在创建一个使用 NZB 文件下载二进制文件的 (C#) 程序,我的应用程序在任何时候都可能只有一个实例在运行。
So when a user doubleclicks an .nzb-file and my program is not running, it should start and process it (easy, file registration). Now if my program is already running, I do NOT want to launch a second instance - I want the already-running instance to pick up the specified file.
因此,当用户双击 .nzb 文件并且我的程序没有运行时,它应该启动并处理它(简单,文件注册)。现在,如果我的程序已经在运行,我不想启动第二个实例 - 我希望已经运行的实例获取指定的文件。
Making my app single-instance can be done using the Visual Basic DLL with the .IsSingleInstance trick, but I don't want to go there.
可以使用带有 .IsSingleInstance 技巧的 Visual Basic DLL 使我的应用程序成为单实例,但我不想去那里。
The right way seems to be to use a mutex to ensure my app is single-instance, but now I'm stuck on how to pass the specified parameter (the .nzb file) to the already-running instance.
正确的方法似乎是使用互斥锁来确保我的应用程序是单实例,但现在我被困在如何将指定的参数(.nzb 文件)传递给已经运行的实例。
Help would be appreciated ! :-)
帮助将不胜感激!:-)
回答by Aric TenEyck
Try this: Your main program creates a named pipe or other interprocess communication facility and listens on it. You create a separate small program that, when run with an NZB file as a parameter, opens the pipe, feeds the file info into it, and exits. Your main program then processes the new file and downloads it.
试试这个:你的主程序创建一个命名管道或其他进程间通信设施并监听它。您创建了一个单独的小程序,当以 NZB 文件作为参数运行时,它会打开管道,将文件信息输入其中,然后退出。然后您的主程序处理新文件并下载它。
回答by RedFilter
Why not have one program that adds the file to a queue, and then kicks off the downloading program if it is not already running. That downloading program watches the queue, which is just a file that you append download file names to.
为什么不用一个程序将文件添加到队列中,然后在下载程序尚未运行时启动它。该下载程序监视队列,队列只是您将下载文件名附加到的文件。
回答by Mike Marshall
回答by Autodidact
Use an IPC (inter process communication) mechanism such as .net remoting
使用 IPC(进程间通信)机制,例如.net 远程处理