Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by jlwilliams1
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...