[英]How to detect if my program is running in Windows?
我正在编写一个C#控制台应用程序。 它将作为计划任务运行。
如果它发现以前的计划任务执行仍在运行另一个进程,我希望我的EXE快速退出。
我似乎找不到找到让我的应用检测活动进程并因此知道它是否已在运行的方法。
感谢您的任何想法。 彼得
一种非常常见的技术是在过程开始时创建互斥体。 如果无法创建互斥锁,则意味着有另一个实例正在运行。
这是Nathan's Link的示例:
//Declare a static Mutex in the main form or class...
private static Mutex _AppMutex = new Mutex(false, "MYAPP");
// Check the mutex before starting up another possible instance
[STAThread]
static void Main(string[] args)
{
if (MyForm._AppMutex.WaitOne(0, false))
{
Application.Run(new MyForm());
}
else
{
MessageBox.Show("Application Already Running");
}
Application.Exit();
}
尝试System.Diagnostics.Process
getProcesses()
使用互斥锁:
// Allow only one instance of app
// See http://www.yoda.arachsys.com/csharp/faq/#one.application.instance
bool firstInstance;
_mutex = new Mutex(false, "Local\\[UniqueName]", out firstInstance);
System.Diagnostics中没有用于检查进程的内容吗?
编辑:这些链接中的任何一个对您有帮助吗? 我可以看到它们可能有用。
很抱歉,我只能为您提供其他项目的链接。 我从未使用过System.Diagnostics,但希望它可以对您或其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.