如何知道外部应用程序是否关闭? [英] How to know if external application was closed?

查看:88
本文介绍了如何知道外部应用程序是否关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能说如果关闭了winform呢...?

How can I say if a winform whas closed do ...?

bool isRunning = false;
foreach (Process clsProcess in Process.GetProcesses()) 
{
    if (clsProcess.ProcessName.Contains("Notepad"))
    {
        isRunning = true;
        break;
    }
}

上面的代码始终检查该进程是否存在,但是代码执行所需的速度很慢。因此,有没有办法检查记事本进程是否真正关闭,而不是总是循环查看它是否在那里?

The code above always checks if the process exists but the code is slow for what I want it to do.So is there a way to check if the Notepad process was actually closed instead of always looping to see if its there?

推荐答案

您可以使用 Win32_ProcessStopTrace 表示某个进程正在

You can use Win32_ProcessStopTrace which indicates that a process is terminated.

ManagementEventWatcher watcher;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    watcher = new ManagementEventWatcher("Select * From Win32_ProcessStopTrace");
    watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
    watcher.Start();
}

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
    if ((string)e.NewEvent["ProcessName"] == "notepad.exe")
        MessageBox.Show("Notepad closed");
}

protected override void OnFormClosed(FormClosedEventArgs e)
{
    watcher.Stop();
    watcher.Dispose();
    base.OnFormClosed(e);
}

别忘了添加对 System的引用管理,并使用System.Management添加

Don't forget to add a reference to System.Management and add using System.Management;

注意


  • 如果要监视某个记事本实例的关闭,可以使用以下条件: / p>

  • If you want to monitor closing of an specific instance of notepad which you know, you can use such criteria:

if ((UInt32)e.NewEvent["ProcessID"]==knownProcessId)


  • 如果要检查是否打开了记事本的任何实例,则可以使用以下条件:

  • If you want to check if any instance of notepad is open, you can use such criteria:

    if (System.Diagnostics.Process.GetProcessesByName("notepad").Any())
    


  • EventArrived 将在与UI线程不同的线程中引发,如果需要操纵UI,则需要使用 Invoke

  • The EventArrived will raise in a different thread than UI thread and if you need to manipulate UI, you need to use Invoke.

    以上方法会通知您有关所有进程关闭的信息,无论它们处于什么时间在应用程序运行之前或之后打开。如果您不希望在应用程序启动后收到有关可能打开的进程的通知,则可以获取现有的记事本进程并订阅其 Exited 事件:

    Above method notifies you about closing of all processes, regardless of the time they are opened, before or after your application run. If you don't want to notified about the processes which may be opened after your application starts, you can get existing notepad processes and subscribe to their Exited event:

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Diagnostics.Process.GetProcessesByName("notepad").ToList()
              .ForEach(p => {
                  p.EnableRaisingEvents = true;
                  p.Exited += p_Exited;
              });
    }
    void p_Exited(object sender, EventArgs e)
    {
        MessageBox.Show("Notepad closed");
    }
    


  • 这篇关于如何知道外部应用程序是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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