Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more: , +
I want to interact with a service and kill a procces whenever this process (executable file) comes up.
I really don't know if that will be an Event or a Handle or this can by made with another way!!!.
But that is what I want is to have a constant monitor in my processes and when a new one tries to start automatically triggers my service.
Is there someone to assist me on this?
Updated 13-Mar-11 9:38am
Sergey Alexandrovich Kryukov 13-Mar-11 15:27pm    
Wow! You share almost no information, and not your goal (first of all, explain your ultimate good with is the main suspect) but still want some answer. Can you imagine yourself in place of the one who answers?
amitkarnik2211 16-Mar-11 5:39am    
Pls be Specific what exatly do u want what have u tried so far
pginis 16-Mar-11 5:49am    
@amitkarnik. I haven't done anything until now except the service which I post it as solution, in order to see where my road drives me so far.
What i want to do is to find a trigger (like a handler or Event) which I'll put it on my code in order to run the killing process.
Now what the service will do I discribed in my question.
Can you assist me please?

1 solution

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Environment
Imports System.ComponentModel
Imports System.Configuration
Imports System.Data
Imports System.Web.Services
Imports System.Runtime.Remoting.Services
Imports System.Diagnostics
Imports System.ServiceProcess
Imports System.IO
Imports System.Runtime.InteropServices
Public Class WPP : Inherits System.ServiceProcess.ServiceBase
    Public myArgs() As String
    Protected Overrides Sub OnStart(ByVal args() As String)
        StartLogFile("WPP Service Started {" & args(0) & "}, {" & args(1) & "}", EventLogEntryType.Information)
        ' Add code here to start your service. This method should set things
        Catch ex As Exception
            StartLogFile("myArgs.Count=" & args.Count & vbNewLine & ex.Message, EventLogEntryType.Error)
        End Try
    End Sub
    Private Sub LoopToKill(ByVal Arguments() As String)
        StartLogFile("WPP Service Started to Kill file {" & Arguments(1) & "}", EventLogEntryType.Information)
        Dim myTime As Double = CDbl(Arguments(0))
        Dim myFile As String = Arguments(1)
            Dim nowTime As New TimeSpan
            nowTime = Today.TimeOfDay
            Dim afterTime As New TimeSpan
            afterTime = TimeSpan.FromSeconds(myTime)
            Dim compTime As Integer = TimeSpan.Compare(nowTime, afterTime)
            While Not compTime = 0
                Do Until compTime = 0
                    Threading.Thread.Sleep(1000) ' 1second
                    nowTime += TimeSpan.FromSeconds(1)
                    compTime = TimeSpan.Compare(nowTime, afterTime)
                nowTime = Today.TimeOfDay
                compTime = -1
            End While
        Catch ex As System.FormatException
            StartLogFile("{" & myTime & "}, {" & myFile & "}, myArgs.Count=" & Arguments.Count & vbNewLine & ex.Message, EventLogEntryType.Error)
        End Try
    End Sub
    Protected Overrides Sub OnStop()
        StartLogFile("WPP Service Stopped", EventLogEntryType.Information)
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub
    Protected Overrides Sub OnPause()
        StartLogFile("WPP Service Paused", EventLogEntryType.Information)
    End Sub
    Protected Overrides Sub OnContinue()
        StartLogFile("WPP Service Resumed", EventLogEntryType.Information)
    End Sub
    Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
        StartLogFile("Custom Command {" & command & "} invoked", EventLogEntryType.Information)
    End Sub
    Private Sub StartLogFile(ByVal msg As String, ByVal myEvent As EventLogEntryType)
        Dim MyLog As New EventLog ' create a new event log 
        ' Check if the the Event Log Exists 
        If Not Diagnostics.EventLog.SourceExists("WPProtect") Then
            Diagnostics.EventLog.CreateEventSource("WPProtect", "WPProtect", ".") ' Create Log 
        End If
        MyLog.Source = "WPProtect"
        MyLog.WriteEntry("[" & MyLog.Source & "] " & vbNewLine & "Message := " & msg & " " & CStr(TimeOfDay), myEvent)
    End Sub
    Private Sub KillTheProcess(ByVal myString As String)
        Dim MyProcess As New Process
        ' You need to be smarter here!
        For Each MyProcess In Process.GetProcesses(System.Net.Dns.GetHostName)
            If MyProcess.ProcessName Like myString & "*" Then
                StartLogFile(" {" & MyProcess.ProcessName & "} -- Killed from (" & System.Net.Dns.GetHostName & ")  ", EventLogEntryType.Warning)
            End If
    End Sub
End Class
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900