在vb.net中处理创建通知 [英] Process Create Notification in vb.net
本文介绍了在vb.net中处理创建通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想与服务进行交互,并在此进程(可执行文件)出现时杀死进程.
我真的不知道这是事件还是句柄,或者可以通过其他方式实现!!!
但这就是我想要在进程中保持不变的监控器,并且当一个新的尝试启动时会自动触发我的服务.
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?
推荐答案
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
这篇关于在vb.net中处理创建通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文