开发者

Restoring and passing data to an already running instance of a .NET application

The goal is to have an application that runs in the system tray and can either accept user input from its actual GUI (which isn't the actual issue) OR accept command line parameters (that would actually be done via a context menu in windows exp开发者_如何学运维lorer). Now, while I'm aware that the command line parameters are not exactly possible once the application has started, I need a way to pass data to the already running application instance via some form of handler. I'm thinking maybe define and raise some sort of event?


You could pass new command line in depending how you handle multiple instances, if when the process discovers another instance is running you could use any IPC method such as sockets or memory mapped files to pass along the info to the existing process before terminating.


You're going to have 2 instances of your program running at the same time. Firing off a normal C# event between them wouldn't work.

The first thing you'll need to do is check to see if the system tray instance is already running. Using a Mutex is probably the simplest way to do it, see this question for details: .NET 4 single application instance

Next, you'll need to pass whatever data you have from the brand-new instance to the listening instance, using .NET remoting, WCF, or some other form of IPC. You'll find a lot of talk about using WCF to talk between machines - don't let that confuse you, or make you think WCF is too heavyweight - using named pipes or even a TCP port listening on localhost will work just fine for what you're doing. You could also skip the mutex entirely and use whatever remoting solution you decide on to check if there's an instance already running.

Also, don't forget to handle the scenario where the user launches your app from the context menu, but the system tray instance isn't running yet (right after a reboot, for example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜