Servizio Windows – Visual Basic 2010 – Framework 4

Di seguito del codice Visual Basic che illustra un semplicissimo servizio Windows che scrive nel log.

Imports System.Timers

Public Class Service1

    Private aTimer As New System.Timers.Timer
    Private myLog As New EventLog()

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Aggiungere il codice qui per iniziare il vostro servizio.

        If Not EventLog.SourceExists(My.Application.Info.AssemblyName) Then
            'Creo il Log, ATTENZIONE IL NOME DEL LOG E` IL SECONDO PARAMETRO DEL METODO CreateEventSource
            EventLog.CreateEventSource(My.Application.Info.AssemblyName, "Log_" & My.Application.Info.AssemblyName)
        End If

        myLog.Source = My.Application.Info.AssemblyName

        myLog.WriteEntry("Servizio Avviato.")

        ' Creare un timer con un intervallo di dieci secondi.
        aTimer = New System.Timers.Timer(10000)

        ' Collegare l'evento trascorso per il timer.
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Impostare l'intervallo di 2 secondi (2000 millisecondi).
        aTimer.Interval = 2000
        aTimer.Enabled = True


    End Sub

    Protected Overrides Sub OnStop()
        ' Aggiungere il codice qui per effettuare qualsiasi chiusura necessaria per interrompere il servizio.
        myLog.WriteEntry("Servizio Arrestato.")
    End Sub

    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        myLog.WriteEntry("L'evento è trascorso è stato sollevato in " & e.SignalTime)
    End Sub

End Class
This entry was posted in Microsoft .NET Visual Basic, Programming Languages. Bookmark the permalink.