Need help in writing a windows service


Mar 15, 2005
Programming Experience
I'm trying to write a windows service routine in VB.NET and I'm using code taken straight from several examples that I've gotten from the web. The problem is that I can compile and install the service ok--it shows up in the services window from the admin tools, but it doesn't seem to be doing what it's supposed to do. Basically, I just wrote a routine that uses a timer with a 10 second tick and on every tick, an entry is supposed to written to the event log and to a text file. The strange thing is that, if I have this service up and running and I then run the program in the VS.NET IDE in debug mode, then it does what it's supposed to do--write an entry to the event log and to a text file every 10 seconds. Very strange. It just seems like nothing is happening when it is just running as a service.

Here's my code:

 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 i As Integer
		Dim na() As String
		Dim DataWatcherLog As New EventLog
		SelectStatement = "SELECT * FROM Test WHERE Newrecord = 'Y'"
		UpdateStatement = "UPDATE Test SET Newrecord = 'N' WHERE Newrecord = 'Y'"
		cmd.CommandText = SelectStatement
		cmd.CommandType = CommandType.Text
		da.SelectCommand = New SqlCommand(SelectStatement, connect)
		da.Fill(ds, "Test")
		If ds.Tables(0).Rows.Count <= 0 Then
			Exit Sub
		End If
		numrecs = ds.Tables(0).Rows.Count
		For i = 1 To numrecs
			' na(numrecs) = ds.Tables("Test").Rows(numrecs - 1).Item("Address")
			emailmsg = emailmsg + " " + "New Record"
		If Not DataWatcherLog.SourceExists("DataWatcher") Then
			DataWatcherLog.CreateEventSource("DataWatcher", "DataWatcher Log")
		End If
		DataWatcherLog.Source = "DataWatcher"
		DataWatcherLog.WriteEntry("DataWatcher Log", emailmsg)
		' Create an instance of StreamWriter to write text to a file.
		Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
		' Add some text to the file.
		sw.Write("This is the ")
		sw.WriteLine("header for the file.")
		' Arbitrary objects can also be written to the file.
		sw.Write("The date is: ")
	End Sub

Can someone toss me a clue or hint please. Thanks.
Problem solved. Apparently the timer control is geared to work with windows forms and does not work correctly with windows services, so I use the system.timer instead.