[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

Visual basic Service to check if a process is running every 10 seconds and kill

Hi
I need to write a windows service that will kill another windows process, the servie will need to check if the process is running every 10 seconds or so.
Can someone give me a start on doing this?
0
sykotex
Asked:
sykotex
2 Solutions
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, create a Windows Service project, add a Timer to the designer, set it interval to 10000 (10 seconds), then add this code to your Service.vb:
Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Start the timer
        Timer1.Start()

        ' Call immediately
        Timer1_Tick(Me, System.EventArgs.Empty)
    End Sub

    Protected Overrides Sub OnStop()
        'Stops the timer
        Timer1.Stop()
    End Sub

    'This find the process that match the name (ex this will kill the task manager instances opened)
    Sub FindAndKill()
        Dim processNameToKill As String = "taskmgr"
        For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName(processNameToKill)
            p.Kill()
        Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        FindAndKill()
    End Sub
End Class

Open in new window


Or you can download the project from this link:
http://dl.dropbox.com/u/13237304/KillProcessService.zip

Inside you can customize the name of the process to be closed (killed).

I hope this help! :)
0
 
Mohamed AbowardaSoftware EngineerCommented:
Add System.Diagnostic namespace
Add timer and set its interval 10000

Add the following code in the timer tick event:
Dim processName As String = "PutProcessNameHere"

For Each p As Process In Process.GetProcesses
    If p.ProcessName = processName Then
       p.Kill()
    End If
Next

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now