Link to home
Start Free TrialLog in
Avatar of grogo21
grogo21

asked on

Windows Service

Hello, I am new to creating windows services and this is my first attempt.  I created a simple service which I want to write text to a log every 5 seconds. Here is the code:



Imports System.IO

Public Class Service1

    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.
        Timer1.Enabled = True
    End Sub

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


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim FILENAME As String = "C:\Inetpub\vhosts\domain.com\httpdocs\ServiceTest.txt"
        Dim objStreamWriter As StreamWriter

        objStreamWriter = File.AppendText(FILENAME)
        objStreamWriter.WriteLine("")
        objStreamWriter.WriteLine("5 Seconds Elapsed")
        objStreamWriter.Close()
        objStreamWriter.Dispose()
    End Sub
End Class
   
I was able to install the service and it is running but it is not writing to the log.  The text file does exists and the timer is set to 5 seconds.  What am I doing wrong?



Thanks Very Much!!



ASKER CERTIFIED SOLUTION
Avatar of MichaelLandauer
MichaelLandauer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nsanga
nsanga

try to add debug statements to a log file ( just define a member variable streamwriter and use it every where )every where.....whether the timer is started........use try catch blocks and write the exceptions to the log file

Avatar of grogo21

ASKER

It is a System.Windows.Forms.Timer but How can I add a System.Timers.Timer.  I dont know where the code is that contains the forms.timer?  IM using Visual studio 2005.  Thanks!
Avatar of grogo21

ASKER

Added it through tools > choose toolbox items.

It works now.  Thanks Very Much!