allora, premesso che sto lavorando in vb, ho convertito il codice:
se ho capito bene midnighttimer e program sono due classi mentre il servizio vero e proprio è EventLogService
Pertanto io ho convertito in vb in questo modo:classe midnightTimer.vb (ho evidenziato l'unica differenza di codice sostanziale che mi pare di rilevare)
codice:
Imports Microsoft.Win32
Imports System.Threading
Imports System.Timers
Friend Class MidnightTimer
Friend Event Elapsed As EventHandler
Private timer As Timers.Timer
Private previousRun As DateTime
Friend Sub New()
AddHandler SystemEvents.TimeChanged, AddressOf SystemEvents_TimeChanged
timer = New Timers.Timer()
timer.AutoReset = False
AddHandler timer.Elapsed, AddressOf timer_Elapsed
End Sub
Friend Sub Start()
previousRun = DateTime.Today
Dim timeSpanToMidnight As TimeSpan = GetNextMidnight().Subtract(DateTime.Now)
timer.Interval = timeSpanToMidnight.TotalMilliseconds
timer.Start()
End Sub
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
If previousRun <> DateTime.Today Then RaiseEvent Elapsed(Me, EventArgs.Empty)
timer.[Stop]()
Start()
End Sub
Private Sub SystemEvents_TimeChanged(ByVal sender As Object, ByVal e As EventArgs)
timer.[Stop]()
Start()
End Sub
Private Shared Function GetNextMidnight() As DateTime
Return DateTime.Today.AddDays(1)
End Function
End Class
classe program.vb (nessun problema)
codice:
Imports System.ServiceProcess
Namespace ScanEventLogService
Module Program
Private Sub Main()
Dim ServicesToRun As ServiceBase()
ServicesToRun = New ServiceBase() {New EventLogService()}
ServiceBase.Run(ServicesToRun)
End Sub
End Module
End Namespace
Il problema ce l'ho nel servizio, non so bene perchè ma ho evidenziato le due istruzioni che VS mi segna come errore
EventLogService.vb
codice:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Diagnostics
Imports System.Linq
Imports System.ServiceProcess
Imports System.Text
Imports System.Threading.Tasks
Imports System.Timers
Imports System.Windows.Threading
Imports System.Configuration
Imports System.IO
Imports System.Net.Mail
Imports System.Net
Namespace ScanEventLogService
Partial Public Class EventLogService
Inherits ServiceBase
Private timer As Timer
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub OnStart(ByVal args As String())
Dim UomoInFrack As MidnightTimer = New MidnightTimer()
UomoInFrack.Elapsed += Function(s, e) OnTimer()
UomoInFrack.Start()
End Sub
Public Sub OnTimer()
Try
'codice da eseguire
Catch ex As Exception
End Try
End Sub
Protected Overrides Sub OnStop()
'altro codice da eseguire
End Sub
End Class
End Namespace
Initializecomponent() non so pproprio...
Uomoinfrack.elapsed sembra non possa essere chiamato in quel modo, dovrei usare un raisevent ma non accetta poi la chiamata del metodo...