如何知道进程是否正在运行? [英] How can I know if a process is running?

查看:36
本文介绍了如何知道进程是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我获得对 System.Diagnostics.Process 的引用时,我如何知道进程当前是否正在运行?

When I get a reference to a System.Diagnostics.Process, how can I know if a process is currently running?

推荐答案

这是一种用名字来做的方法:

This is a way to do it with the name:

Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
  MessageBox.Show("nothing");
else
  MessageBox.Show("run");

您可以循环所有进程以获取 ID 以供以后操作:

You can loop all process to get the ID for later manipulation:

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
   Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}

这篇关于如何知道进程是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆