1 Replies - 2025 Views - Last Post: 15 March 2013 - 11:56 AM Rate Topic: -----

#1 VBbeginner01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-November 12

Process watcher

Posted 15 March 2013 - 05:55 AM

I am working at a function that monitors the processes so when a new process is opened (even from the same location , like a application to be opened twice) , it will tell me the process and filename of the process that has been opened.

What have i done so far:
Dim processesList As String 'Containts initial processes list.
    Dim processActivity As String 'Containts all opened / closed processes.
    Dim processesAtStart As New ArrayList 'Original Processes List as ArrayList. (Used to Compare)
    Dim processesCheck As New ArrayList 'Latest Processes List as ArrayList. (Used to Compare)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim proc As New Process
        For Each proc In Process.GetProcesses
            Try
                processesList = processesList + proc.MainModule.ModuleName + " - " + proc.MainModule.FileName + ControlChars.NewLine
                processesAtStart.Add(proc.MainModule.ModuleName + " - " + proc.MainModule.FileName)
            Catch ex As Exception
                processesList = processesList + proc.ProcessName + ControlChars.NewLine
                processesAtStart.Add(proc.ProcessName)
            End Try
        Next
        proc.Dispose()
    End Sub


    Public Sub processCheck()
        'Gets latest processes list and compares it to initial list.
        processesCheck.Clear()
        Dim proc As New Process
        For Each proc In Process.GetProcesses
            Try
                processesCheck.Add(proc.MainModule.ModuleName + " - " + proc.MainModule.FileName)
            Catch ex As Exception
                processesCheck.Add(proc.ProcessName)
            End Try
        Next
        proc.Dispose()
        'Convert list into array to be compared
        Dim latestProcesses() As String = DirectCast(processesCheck.ToArray(GetType(String)), String())
        Dim initialProcesses() As String = DirectCast(processesAtStart.ToArray(GetType(String)), String())
        'If process is closed remove from list
        For Each item As String In initialProcesses
            If (Array.IndexOf(latestProcesses, item) = -1) Then
                processesAtStart.Remove(item)
            End If
        Next
'Find Opened processes
 For Each item As String In latestProcesses
            If (Array.IndexOf(initialProcesses, item) = -1) Then
                processActivity = processActivity + ControlChars.NewLine + "Opened : " + item
                processesAtStart.Add(item)
            End If
        Next
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        processCheck()
        TextBox1.Text = processActivity
    End Sub



Problems:
1.If the same process is opened again at the same time, it won't notify me
2.resource eater (added at 500 ms and takes about 5-10% CPU of a quad core and over 25 mb RAM)

Is This A Good Question/Topic? 0
  • +

Replies To: Process watcher

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 281
  • View blog
  • Posts: 891
  • Joined: 01-February 13

Re: Process watcher

Posted 15 March 2013 - 11:56 AM

Hello VBbeginner01,
It will not add 2 of the same named processes because, the way the following code checks the new processes and adds them. Say i open Notepad.exe. The timer ticks and it adds it to the (latestProcesses) Array but, its not in the (initialProcesses) Array yet. So these lines say if the IndexOf (Notepad.exe) is not found in (initialProcesses) then add it to the textbox.
        For Each item As String In latestProcesses

            'This adds it if it is not in initialProcesses already but, the second duplicate program name
            'is not added because initialProcesses already has that name in it from the 1st program opened.
            If (Array.IndexOf(initialProcesses, item) = -1) Then
                processActivity = processActivity + ControlChars.NewLine + "Opened : " + item
                processesAtStart.Add(item)
            End If
        Next


Now i open another Notepad.exe and then the timer ticks again and the first Notepad.exe i opened is already in (initialProcesses) so when it checks the IndexOf (Notepad.exe) it finds the first one i opened and does not add the second one.
If you want to check what i am saying about the timer tick and adding notepad to (initialProcesses) you can set your timer1 to have an interval of 30 seconds. Run your program and open 2 notepad windows real quick. They both get added to the textbox.

This post has been edited by IronRazer: 15 March 2013 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1