-
Aug 11th, 2013, 05:28 AM
#1
Thread Starter
Fanatic Member
[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
-
Aug 11th, 2013, 05:47 AM
#2
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.
-
Aug 11th, 2013, 05:51 AM
#3
Thread Starter
Fanatic Member
Re: detect when a process starts?
I will look into WMI and post back when I've done so.
-
Aug 11th, 2013, 05:52 AM
#4
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
Last edited by ident; Aug 11th, 2013 at 06:19 AM.
Reason: added link
-
Aug 11th, 2013, 06:03 AM
#5
Re: detect when a process starts?
Edited the query from the link above to watch an individual process.
vb Code:
Imports System.Management Public Class Form1 Private Sub MonitorProcess(ByVal processName As String) ' Create event query to be notified within 1 second of ' a change in a service Dim query As String query = "SELECT * FROM" & _ " __InstanceCreationEvent WITHIN 1 " & _ "WHERE TargetInstance isa ""Win32_Process""" & _ " AND TargetInstance.Name = '" & processName & "'" ' Event options Dim eventOptions As New EventWatcherOptions eventOptions.Timeout = System.TimeSpan.MaxValue ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher( _ "root\CIMV2", query, eventOptions) Dim e As ManagementBaseObject = _ watcher.WaitForNextEvent() 'Display information from the event Debug.WriteLine( _ "Process {0} has created, path is: {1}", _ CType(e("TargetInstance"), _ ManagementBaseObject)("Name"), _ CType(e("TargetInstance"), _ ManagementBaseObject)("ExecutablePath")) 'Cancel the subscription watcher.Stop() End Sub End Class
-
Aug 11th, 2013, 06:33 AM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|