jlwilliams1 Posted March 16, 2005 Posted March 16, 2005 (edited) Need help with a Windows Service Sorry if this is the wrong place to post this, but I didn't see any category that included Windows Services. 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 Timer1.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Stop() 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.SelectCommand.Connection.Open() da.Fill(ds, "Test") If ds.Tables(0).Rows.Count <= 0 Then da.SelectCommand.Connection.Close() 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" Next If Not DataWatcherLog.SourceExists("DataWatcher") Then DataWatcherLog.CreateEventSource("DataWatcher", "DataWatcher Log") End If DataWatcherLog.Source = "DataWatcher" DataWatcherLog.WriteEntry("DataWatcher Log", emailmsg) da.SelectCommand.Connection.Close() ' 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.") sw.WriteLine("-------------------") ' Arbitrary objects can also be written to the file. sw.Write("The date is: ") sw.WriteLine(DateTime.Now) sw.Write(emailmsg) sw.Close() End Sub Can someone toss me a clue or hint please. Thanks. Edited March 16, 2005 by jlwilliams1 Quote
Administrators PlausiblyDamp Posted March 16, 2005 Administrators Posted March 16, 2005 You may want to look at using a System.Threading.Timer instead of the timer component - I think the problem lies with the service not having a wnidow and therefore not responding to the WM_TIMER message windows should be sending. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jlwilliams1 Posted March 16, 2005 Author Posted March 16, 2005 Any idea on how I would incorporate that into my code? I looked through the sample provided by Microsoft and as usual, just got lost as their samples usually are not very easy and simple to read. Quote
jlwilliams1 Posted March 16, 2005 Author Posted March 16, 2005 Problem solved. I used the System.Timer instead and it works fine. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.