2012-03-07 40 views

回答

1

好的,当你双击快捷方式时,它实际上会打开应用程序的另一个实例,它不知道已经最小化到托盘的应用程序。

本质上你想检测到你的应用的另一个实例在启动时运行。如果是,告诉现有的应用程序实例向用户显示它的UI,然后退出。

您的解决方案包括两个方面:

  1. 的能力,为您的应用程序要明白,它的另一个实例已在运行。
  2. 您的应用程序能够“交谈”(进程间通信)到其他实例并告诉他们该做什么的能力。

1.您的应用程序能够理解其另一个实例已在运行的能力。
这在.NET中很简单。当您打开应用程序时,请使用Mutex类。这是一个全系统锁定,其性质与Monitor类似。

实施例:

// At app startup: 
bool createdNew; 
var mutex = new Mutex(true, Application.ProductName, out createdNew); 
if (!createdNew) 
{ 
    // Use IPC to tell the other instance of the app to show it's UI 
    // Return a value that signals for the app to quit 
} 

// At app shutdown (unless closing because we're not the first instance): 
mutex.ReleaseMutex(); 

2.进程间通信
存在用于在.NET做IPC几个方法。 WCF虽然很重要,命名管道可能是您的最佳选择,虽然它是一个如此简单的要求,即基本套接字消息也应该起作用。

这里有一个问题一个链接在.NET适当的IPC方法来帮助你:What is the best choice for .NET inter-process communication?

+0

你不*有*的进程间通信,只要找到正确的流程并激活窗口.. – stuartd 2012-03-07 12:56:48

+0

啊好吧。所以我知道你可以迭代这个过程,但是你怎么激活这个窗口? – 2012-03-07 15:07:45

+0

我使用这个:https://gist.github.com/1993803 – stuartd 2012-03-07 15:29:48

相关问题