【发布时间】:2013-01-28 10:35:17
【问题描述】:
可能重复:
What is the correct way to create a single instance application?
How to implement single instance per machine application?
我有一个 Winform 应用程序,它接受一个参数作为参数。
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Gauges(args));
}
}
这个程序由另一个应用程序一天执行几次。
是否可以检查我的程序是否已经在运行,如果是,我可以使用带有最新参数的正在运行的实例吗?
【问题讨论】:
-
通过获取所有进程来实施检查,并将进程名称与您正在启动的进程名称进行比较。如果它出现在列表中,只需
return;。如果不是 - 启动它 -
@t3hn00b 只需使用互斥锁。不需要检查所有进程。
-
@Lloyd 是的,没错——在看到你的帖子之前,我并不熟悉 Mutex 类。