I am using c# windows application.
我正在使用c#windows应用程序。
when my application is started up, i want to check the instances in the taskmanager for the same application. If any instances running already, shud kill the process and start the new one.
当我的应用程序启动时,我想检查任务管理器中的实例是否为同一个应用程序。如果任何实例已经运行,则shud会终止进程并启动新进程。
This is to make shure that only one instances of application is running.
这是为了确保只有一个应用程序实例正在运行。
4 个解决方案
#1
Here's a quick and dirty way to make sure only one instance of your program is running. The secret is using a Mutex.
这是一种快速而又脏的方法,可确保只运行一个程序实例。秘密是使用互斥锁。
[STAThread]
static void Main()
{
bool ok;
m = new System.Threading.Mutex(true, "YourNameHere", out ok);
if (!ok)
{
MessageBox.Show("Another instance is already running.");
return;
}
// Do some stuff here...
GC.KeepAlive(m);
}
This code tries to create a Mutex (mutual exclusion) with a certain name and if it can't create it then an instance of this program must already be running.
此代码尝试创建具有特定名称的Mutex(互斥),如果无法创建它,则此程序的实例必须已在运行。
#2
To me that sounds really aggressive and is probably not the best idea.
对我来说听起来很有侵略性,可能不是最好的主意。
Perhaps you want to maintain a single instance of your app.
也许您想要维护应用程序的单个实例。
#3
public Process[] ExistingProcesses()
{
string processName = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(processName);
return processes;
}
#4
Rather than depending on process name, which can be defeated even accidentally by renaming the executable, the traditional way to check for an existing instance is to have a named handle, such as a mutex. During startup, check if it already exists, shutting down immediately if it does.
而不是依赖于进程名称,即使通过重命名可执行文件而偶然失败,检查现有实例的传统方法是使用命名句柄,例如互斥锁。在启动期间,检查它是否已经存在,如果存在则立即关闭。
edit
I just read the question again and it sounds like you really want to kill the previous instance, not kill the new one. The closest I can remember seeing to that behavior is what Google Chrome does when you open a new window and the old one is unresponsive, but even that checks for the lack of response and then prompts the user. Killing the old process sounds a bit extreme. Are you sure that's what you want to do?
我刚刚再次阅读了这个问题,听起来你真的想要杀死之前的实例,而不是杀掉新的实例。我记得最接近这种行为的是谷歌浏览器在你打开一个新窗口时所做的事情,旧的窗口没有响应,但即便检查缺少响应,然后提示用户。杀死旧过程听起来有点极端。你确定那是你想做的吗?
#1
Here's a quick and dirty way to make sure only one instance of your program is running. The secret is using a Mutex.
这是一种快速而又脏的方法,可确保只运行一个程序实例。秘密是使用互斥锁。
[STAThread]
static void Main()
{
bool ok;
m = new System.Threading.Mutex(true, "YourNameHere", out ok);
if (!ok)
{
MessageBox.Show("Another instance is already running.");
return;
}
// Do some stuff here...
GC.KeepAlive(m);
}
This code tries to create a Mutex (mutual exclusion) with a certain name and if it can't create it then an instance of this program must already be running.
此代码尝试创建具有特定名称的Mutex(互斥),如果无法创建它,则此程序的实例必须已在运行。
#2
To me that sounds really aggressive and is probably not the best idea.
对我来说听起来很有侵略性,可能不是最好的主意。
Perhaps you want to maintain a single instance of your app.
也许您想要维护应用程序的单个实例。
#3
public Process[] ExistingProcesses()
{
string processName = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(processName);
return processes;
}
#4
Rather than depending on process name, which can be defeated even accidentally by renaming the executable, the traditional way to check for an existing instance is to have a named handle, such as a mutex. During startup, check if it already exists, shutting down immediately if it does.
而不是依赖于进程名称,即使通过重命名可执行文件而偶然失败,检查现有实例的传统方法是使用命名句柄,例如互斥锁。在启动期间,检查它是否已经存在,如果存在则立即关闭。
edit
I just read the question again and it sounds like you really want to kill the previous instance, not kill the new one. The closest I can remember seeing to that behavior is what Google Chrome does when you open a new window and the old one is unresponsive, but even that checks for the lack of response and then prompts the user. Killing the old process sounds a bit extreme. Are you sure that's what you want to do?
我刚刚再次阅读了这个问题,听起来你真的想要杀死之前的实例,而不是杀掉新的实例。我记得最接近这种行为的是谷歌浏览器在你打开一个新窗口时所做的事情,旧的窗口没有响应,但即便检查缺少响应,然后提示用户。杀死旧过程听起来有点极端。你确定那是你想做的吗?