我正在编写一个C#控制台应用程序。 它将按计划的任务运行。
如果发现另一个进程仍在执行上一个计划任务,我希望我的EXE快速退出。
我似乎无法find让我的应用程序检测活动进程的方式,并知道它是否已经运行。
感谢您的任何想法。 彼得
一个非常常见的技术是在您的进程启动时创建一个互斥锁。 如果你不能创建互斥体,这意味着有另一个实例在运行。
这是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,但希望它可以帮助你或其他人。