如何检测我的程序是否在Windows中运行?

时间:2022-05-05 00:39:13

I am writing a C# console app. It's going to run as a scheduled task.

我正在编写一个C#控制台应用程序。它将作为计划任务运行。

I want my EXE to exit quickly if it finds that another process is still running from the previous schedule task execution.

如果发现另一个进程仍在从先前的计划任务执行中运行,我希望我的EXE快速退出。

I can't seem to find the way to let my app detect the active processes, and so know whether it's already running or not.

我似乎找不到让我的应用程序检测活动进程的方法,因此知道它是否已经运行。

Thanks for any ideas. Peter

谢谢你的任何想法。彼得

4 个解决方案

#1


One very common technique is to create a mutex when your process starts. If you cannot create the mutex it means there is another instance running.

一种非常常见的技术是在进程启动时创建互斥锁。如果无法创建互斥锁,则表示正在运行另一个实例。

This is the sample from Nathan's Link:

这是来自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();
}

#2


try System.Diagnostics.Process

getProcesses()

#3


Use a mutex:

使用互斥锁:

// 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);

Link to reference in comment

链接到评论中的参考

#4


Isn't there something in System.Diagnostics for checking processes?

System.Diagnostics中没有用于检查进程的东西吗?

Edit: Do any of these links help you? I can see how they might be useful.

编辑:这些链接中的任何一个对您有帮助吗?我可以看到它们如何有用。

I'm sorry that all I can do is provide you links to other items. I've never had to use System.Diagnostics but hopefully it might help you or someone else out.

对不起,我所能做的就是为你提供其他物品的链接。我从来没有使用过System.Diagnostics,但希望它可以帮助你或其他人。

#1


One very common technique is to create a mutex when your process starts. If you cannot create the mutex it means there is another instance running.

一种非常常见的技术是在进程启动时创建互斥锁。如果无法创建互斥锁,则表示正在运行另一个实例。

This is the sample from Nathan's Link:

这是来自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();
}

#2


try System.Diagnostics.Process

getProcesses()

#3


Use a mutex:

使用互斥锁:

// 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);

Link to reference in comment

链接到评论中的参考

#4


Isn't there something in System.Diagnostics for checking processes?

System.Diagnostics中没有用于检查进程的东西吗?

Edit: Do any of these links help you? I can see how they might be useful.

编辑:这些链接中的任何一个对您有帮助吗?我可以看到它们如何有用。

I'm sorry that all I can do is provide you links to other items. I've never had to use System.Diagnostics but hopefully it might help you or someone else out.

对不起,我所能做的就是为你提供其他物品的链接。我从来没有使用过System.Diagnostics,但希望它可以帮助你或其他人。