检测 exe 何时启动 vb.net [英] Detect when exe is started vb.net

查看:43
本文介绍了检测 exe 何时启动 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何让我的 VB.net 应用程序等到检测到进程正在运行吗?

Dose anybody know how I can make my VB.net application wait until a process is detected as running?

我可以找到如何检测 exe 完成运行后的示例,但没有检测 exe 何时启动的示例?

I can find example of how to detect once an exe has finished running but none that detect when an exe is started?

推荐答案

您可以使用 System.Management.ManagementEventWatcher 等待某些 WMI 事件发生.你需要给它一个查询类型和条件,让它监视你的进程的下一次创建,然后让它在发生时做一些事情.

You can use the System.Management.ManagementEventWatcher to wait for certain WMI events to occur. You need to give it a query type and condition to have it watch for the next creation of your process, then get it to do something when that occurs.

例如,如果你想:

Dim watcher As ManagementEventWatcher

Public Sub Main()
    Dim monitoredProcess = "Notepad.exe"
    Dim query As WqlEventQuery = New WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process"" And TargetInstance.Name = """ & monitoredProcess & """")

    watcher = New ManagementEventWatcher()
    watcher.Query = query

    'This starts watching asynchronously, triggering EventArrived events every time a new event comes in.
    'You can do synchronous watching via the WaitForNextEvent() method
    watcher.Start()

End Sub

Private Sub Watcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles watcher.EventArrived
    'Do stuff with the startup event
End Sub  

最终您需要停止观察者,这可以通过关闭应用程序或调用 watcher.Stop() 来完成.这是作为大脑编译器编写的,所以如果有任何问题,请告诉我.

Eventually you'll need to stop the watcher, which is you can do by closing the app, or calling watcher.Stop(). This has been written as brain compiler, so if there's any issues let me know.

这篇关于检测 exe 何时启动 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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