我是一个MDI文件查看器。也就是说,我的程序可以在单个实例中打开多个文件。顺便说一下,我的应用程序不是单个实例应用程序,因此用户可以随意打开多个实例。
我想要的行为是:
我想要的行为是:
When the user double-clicks a file in Windows Explorer,
if there is an existing instance of my application,
then open the file with that instance
else
open the file with a new instance.
我认为这种行为非常普遍。Internet Explorer 9的工作方式就是如此。因此,我相信已经有很多人在此之前实现过这个功能。是否有任何成熟的.NET(首选C#)示例代码可供使用(如果可能,不要使用Win32 API)?
我猜算法可能类似于以下内容,但我不知道它是否是最佳或最清晰的实现代码(不使用Win32 API)。
At the program's start up
1)If there are arguments in Main(),
check for existing instances.
2)If an instance exists,
send a message to the instance so that it can open the file.
Then exit.
3)else
open the file.
----添加----- 对于那两个回答我的问题的人,请注意,我的应用程序不是单例应用程序!就像Internet Explorer 9一样。我正在研究WCF P2P,因为我必须向运行中的每个实例广播打开文件消息,然后从中选择一个。但是使用WCF P2P似乎需要做很多工作,因为它似乎要打开和监听TCP端口。什么是最佳实践?