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
'LoopToKill(args)
Try
KillTheProcess(args(1))
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)
Try
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)
Loop
KillTheProcess(myFile)
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
MyProcess.Kill()
StartLogFile(" {" & MyProcess.ProcessName & "} -- Killed from (" & System.Net.Dns.GetHostName & ") ", EventLogEntryType.Warning)
Return
End If
Next
End Sub
End Class