检测exe何时启动vb.net

前端之家收集整理的这篇文章主要介绍了检测exe何时启动vb.net前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都知道如何让我的VB.net应用程序等到检测到进程正在运行?

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

您可以使用 System.Management.ManagementEventWatcher等待某些WMI事件发生.您需要为其提供一个查询类型和条件,让它监视您的进程的下一个创建,然后让它在发生时执行某些操作.

例如,如果你想:

Dim watcher As ManagementEventWatcher

Public Sub Main()
    Dim monitoredProcess = "Notepad.exe"
    Dim query As WqlEventQuery = New WqlEventQuery("__InstanceCreationEvent",new TimeSpan(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()来做.这是作为脑编译器编写的,所以如果有任何问题请告诉我.

原文链接:https://www.f2er.com/vb/255760.html

猜你在找的VB相关文章