Results 1 to 6 of 6

Thread: [RESOLVED] detect when a process starts?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] detect when a process starts?

    I'm trying to detect when a certain program is opened, and from searching i found that the way to do it would be using a code as the one below inside a timer.. i just wanted to ask if there was a better way of doing it, or am i doing it correctly?

    Code:
            Dim processes() As Process
            processes = Process.GetProcessesByName("notepad")
    
                For Each Proc In processes
                    If Not ListBox1.Items.Contains(Proc.Id) Then
                        ListBox1.Items.Add(Proc.Id)
                        'do something here
                    End If
                Next

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: detect when a process starts?

    Polling is generally a poor way to do anything. Sometimes it may be necessary but you should always use an alternative if there is one. In this case, there's no simple option for doing what you want to do in managed code. I would expect that there's a way to do it using WMI, which is invoked in managed code via the System.Management namespace. I have no idea what types would be used but I would encourage you to investigate that option as notification has to be better than polling.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: detect when a process starts?

    I will look into WMI and post back when I've done so.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: detect when a process starts?

    * edit tab was open while popped out so just repeated what already been said :/

    Adding processes to a control inside a timer is certainly not the way to do any thing. Controls are there for a reason, not to be used as variables. You can use the WMI creator and importing System.Management namespace.

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: detect when a process starts?

    Edited the query from the link above to watch an individual process.

    vb Code:
    1. Imports System.Management
    2.  
    3. Public Class Form1
    4.     Private Sub MonitorProcess(ByVal processName As String)
    5.  
    6.         ' Create event query to be notified within 1 second of
    7.         ' a change in a service
    8.         Dim query As String
    9.         query = "SELECT * FROM" & _
    10.             " __InstanceCreationEvent WITHIN 1 " & _
    11.             "WHERE TargetInstance isa ""Win32_Process""" & _
    12.             "   AND TargetInstance.Name = '" & processName & "'"
    13.  
    14.  
    15.         ' Event options
    16.         Dim eventOptions As New EventWatcherOptions
    17.         eventOptions.Timeout = System.TimeSpan.MaxValue
    18.  
    19.         ' Initialize an event watcher and subscribe to events
    20.         ' that match this query
    21.         Dim watcher As New ManagementEventWatcher( _
    22.             "root\CIMV2", query, eventOptions)
    23.  
    24.         Dim e As ManagementBaseObject = _
    25.             watcher.WaitForNextEvent()
    26.  
    27.         'Display information from the event
    28.         Debug.WriteLine( _
    29.             "Process {0} has created, path is: {1}", _
    30.             CType(e("TargetInstance"),  _
    31.                 ManagementBaseObject)("Name"), _
    32.             CType(e("TargetInstance"),  _
    33.                 ManagementBaseObject)("ExecutablePath"))
    34.  
    35.         'Cancel the subscription
    36.         watcher.Stop()
    37.  
    38.     End Sub
    39.  
    40. End Class

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: detect when a process starts?

    thank you ident that works very perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width