gpt4 book ai didi

vb.net - window 服务

转载 作者:行者123 更新时间:2023-12-02 06:46:23 24 4
gpt4 key购买 nike

我正在尝试在.NET 3.5中编写一个小型Windows服务,每隔1000万左右检查一次“C:\demofolder”中是否有新文件,然后发送电子邮件。到目前为止,我已经做到了这里,如下面的代码所示,然后 Public Sub New() 中出现错误

Imports System
Imports System.Timers
Imports System.ServiceProcess
Public Class TestMyService

' A timer that controls how frequenty the example writes to the
' event log.
Private serviceTimer As Timer

Public Sub New()

' Set the ServiceBase.ServiceName property.
ServiceName = "TestMyService Service"

' Configure the level of control available on the service.
CanStop = True
CanPauseAndContinue = True
CanHandleSessionChangeEvent = True

' Configure the service to log important events to the
' Application event log automatically.
AutoLog = True

End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

Public Shared Sub Main()

' Create an instance of the TestMyService class that will write
' an entry to the Application event log. Pass the object to the
' shared ServiceBase.Run method.
ServiceBase.Run(New TestMyService)

End Sub


End Class

我收到以下错误消息:

Sub Main' is declared more than once in 'mcWinService.TestMyService': mcWinService.TestMyService.Main(), mcWinService.TestMyService.Main()

Public Shared Sub Main()' has multiple definitions with identical signatures.

Public Sub New()' in designer-generated type 'mcWinService.TestMyService' should call InitializeComponent method.

最佳答案

尝试移动

ServiceBase.Run(New TestMyService) from Public Shared Sub Main()

Protected Overrides Sub OnStart

然后删除 Public Shared Sub Main()

此外,删除 Public Sub New(),因为您可以从“属性窗口”设置这些属性。 F7 从代码切换到设计器 View 。

Update 1: Example of Windows Service for folder monitor

Imports System.Net.Mail
Imports System.Net
Imports System.Timers
Imports System.IO

Public Class DemoFolderMonitor

Private Shared timer As System.Timers.Timer
Private Shared timerInterval As Integer

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

' Use the EventLog object automatically configured by the
' ServiceBase class to write to the event log.
'EventLog.WriteEntry(String.Format("DemoFolderMonitor Service starting."))

' Set the Interval (1sec = 1000 milliseconds).
timerInterval = 2000
timer = New System.Timers.Timer(timerInterval)
EventLog.WriteEntry("DemoFolderMonitor Service starting.")

' Hook up the Elapsed event for the timer.
AddHandler timer.Elapsed, AddressOf WatchFolder

timer.Interval = timerInterval
timer.Enabled = True

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EventLog.WriteEntry("DemoFolderMonitor Service stopping...")

End Sub

Protected Sub WatchFolder()

Dim watcher As New FileSystemWatcher
watcher.Path = "C:\Demo\"

watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)

'watch all file types.
watcher.Filter = "*.*"
' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Created, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnChanged
AddHandler watcher.Renamed, AddressOf OnRenamed

' Begin watching.
watcher.EnableRaisingEvents = True

End Sub

'Define the event handlers.
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
' Specify what is done when a file is changed, created, or deleted.
Dim changeLog = "File: " & e.FullPath & " " & " | Action: " & e.ChangeType.ToString
WriteChangeLog(changeLog)

End Sub

Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Dim changeLog = "File" & e.OldFullPath & " " & "renamed to" & " " & e.FullPath
WriteChangeLog(changeLog)

End Sub

'Custom action. You can either send an e-mail or write change to local file
'In this example I write change to local file
Private Shared Sub WriteChangeLog(ByVal changeLog As String)

'Create a text file and write the change log to the text file
Dim filename As String = DateTime.Now.ToString("hh-mm-ss") & ".txt"
Dim writer As StreamWriter
writer = File.CreateText("C:\ChangeLog\" & filename)
writer.WriteLine("Datetime Log at {0} Log Message: {1}", DateTime.Now.ToString("MMM-dd-yyyy @ hh:mm:ss tt"), changeLog)
writer.Close()

End Sub

End Class

进一步相关阅读检查:

欢呼!

关于vb.net - window 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3447668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com