2

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

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

LabRat
  • 1,996
  • 11
  • 56
  • 91

4 Answers4

4

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.

For example, if you want :

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  

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.

Adrian
  • 2,825
  • 1
  • 22
  • 32
  • I think you'll have to provide an interval: `Dim query As WqlEventQuery = New WqlEventQuery("__InstanceCreationEvent", New TimeSpan(0, 0, 1),"TargetInstance isa ""Win32_Process"" And TargetInstance.Name = """ & monitoredProcess & """")` – sloth Sep 12 '13 at 08:32
  • @DominicKexel thanks, I already mentioned the `WaitForNextEvent()` in my code, in the comment just above `watcher.Start()`. And the interval isn't required. [The constructor I used in my example](http://msdn.microsoft.com/en-us/library/ee2ayw2h.aspx) is also valid. The `TimeSpan` you've specified there really only means that it won't accept events more than one second old. – Adrian Sep 12 '13 at 11:38
  • 1
    Sorry, missed your comment. However, if you don't provide an interval, you'll get a COM-Exception `0x80042002 WBEMESS_E_REGISTRATION_TOO_PRECISE: A WITHIN clause was not used in this query.`; at least I run into this exception on my Win7 machine. – sloth Sep 12 '13 at 11:48
  • Okay, I don't get that error, but it doesn't hurt to have the interval there, so I'll update the example. – Adrian Sep 12 '13 at 11:53
0

You could simply wait and check every once in a while whether the process exists. Use Thread.Sleep to avoid busy waiting.

However, this has the possibility that you miss the process if it starts and exists during your wait time.

Joey
  • 344,408
  • 85
  • 689
  • 683
0

You can use the below condition

return Process.GetProcesses().Any(Function(p) p.Name.Contains(myProcessName))

MansoorShaikh
  • 913
  • 1
  • 6
  • 19
0
   Dim p() As Process

 Private Sub CheckIfRunning()
        p = Process.GetProcessesByName("processName")
        If p.Count > 0 Then
            ' Process is running
        Else
            ' Process is not running
        End If
    End Sub

OR SIMPLY

System.Diagnostics.Process.GetProcessesByName("processName")

VB.NET LEARNER
  • 711
  • 5
  • 11