c#检查进程是否正在运行并正常运行

时间:2015-04-05 15:58:21

标签: c#

要检查应用程序是否正在运行并继续或返回到先前的状态,这将使您的生活更轻松,以便不退出任何当前进程

 protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Process[] pname = Process.GetProcessesByName("ConsoleApplication");
            if (pname.Length == 0)
            {
            }
            else
            {
                MessageBox.Show("You cannot exit until the process has finished");
                e.Cancel = true;
            }

        }

1 个答案:

答案 0 :(得分:1)

您可以检查具有该名称的活动进程:

using System.Diagnostics;
//...
bool isRunning = Process.GetProcessesByName("blocker.exe").Any();

但请注意,您无法强制您的可执行文件仍在运行 - 这只有在不强制关闭您的进程时才会起作用(" kill"),但会正确地要求它自行关闭( "靠近&#34)。

相关问题