Installare un servizio windows

martedì 06 aprile 2004 - 12.14

zangief77 Profilo | Newbie

Salve,

Ho letto il tutorial su come installare il servizio, tutto chiaro, mi sono scaricato l'esempio in vb e funziona correttamente. Ho provato a fare un programmino molto simile per prendere un po' di confidenza usando anche qui un timer come nell'esempio, ma una volta avviato il servizio viene automaticamente arrestato dicendo che non ha alcuna operazione da fare. L'handle del timer l'ho fatto come nell'esempio, ho cambiato soltanto la funzione all'interno del timerfired.

Grazie

GvnnRules Profilo | Senior Member

Prova a postare il codice della funzione, oppure allega la tua prova in toto che magari riusciamo a trovare l'inghippo.

Ciao

Gvnn

zangief77 Profilo | Newbie

Imports System.ServiceProcess
Imports System.IO
Imports System.Configuration

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

Private Timer As New System.Timers.Timer(5000)
Private LogPath, LogFile As String
#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub

#End Region

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.
Load_Variable()
Timer.Enabled = True
End Sub

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

Private Sub Load_Variable()

AddHandler Timer.Elapsed, AddressOf Timer_Elapsed
LogPath = Configuration.ConfigurationSettings.AppSettings("logpath").ToString
LogFile = Configuration.ConfigurationSettings.AppSettings("logfile").ToString

End Sub

Public Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)

Dim sw As New StreamWriter(LogPath & LogFile)
sw.WriteLine(DateTime.Now.ToString & " - Log Riuscito")

End Sub

End Class


Questo è il listato, ho comunque aggiunto il progetto. Grazie a priori per l'interessamento, vi citerò nei ringraziamenti della mia tesi!!

Gianni

GvnnRules Profilo | Senior Member

Problema risolto, ci sono un paio di errori.

1°:
nel file app.config hai sbagliato a scrivere questa riga

<ad key="LogFile" value="sw.txt" />

hai dimenticato una d nella parola add, la riga giusta è

<add key="LogFile" value="sw.txt" />

2°:
nella funzione Timer_Elapsed, dopo che hai scritto la riga devi chiudere lo stream, se no non è possibile scrivere la riga successiva, inoltre per aggiungere una riga nuova ogni volta devi cambiare la chiamata all'oggetto stream writer, ecco il codice:

Dim sw As New StreamWriter(LogPath & LogFile, True)
sw.WriteLine(DateTime.Now.ToString & " - Log Riuscito")
sw.Close()

il valore True che vedi nella chiamata al costruttore indica che vuoi aggiungere una nuova riga, se non lo metti ogni volta ti cancellerà il contenuto precedente e scriverà una sola riga.

Spero che funzioni

Ciao

Gvnn

zangief77 Profilo | Newbie

Grazie Mille!!!!!

Purtroppo non è la prima volta che qualche lettera mi rimane dentro i polpastrelli :-) !!!!
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5