We help IT Professionals succeed at work.

Windows Service Stops Working After Locking the Computer

jbambas
jbambas asked
on
I wrote a windows service in VB.NET that uses a timer to run code every so often.  However, I noticed that when I lock my computer and then log back in, the service doesn't work.  It still shows up in the Processes tab, and according to the Services manager, it is still "started".  The account is set to "LocalSystem" and the StartType is set to "Automatic".  Any ideas?  Below is a snippet of my code.

Imports System.IO
Public Class BBParserService

  Private WithEvents parseTimer As New Timers.Timer

  Protected Overrides Sub OnStart(ByVal args() As String)
    parseTimer.Interval = 30000
    parseTimer.Enabled = True
    parseTimer.Start()
  End Sub

  Protected Overrides Sub OnStop()
    parseTimer.Stop()
    parseTimer.Dispose()
  End Sub

  Private Sub parseTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles parseTimer.Elapsed
    Dim fs As New FileStream("\\fileserver1\data\BBParser\log.txt", FileMode.OpenOrCreate, FileAccess.Write)
    Dim sw As New StreamWriter(fs)
    sw.BaseStream.Seek(0, SeekOrigin.End
    sw.WriteLine("testing")
    sw.Flush()
    sw.Close()
    fs.Close()
  End Sub
End Class

Open in new window

Comment
Watch Question

Vaughn BighamSr. Software Engineer / Architect

Commented:
You should probably use a while loop with a background worker that sleeps its thread... Something like this:
(note: This might be slightly pseudo code)

Imports System.IO

Public Class BBParserService

    Private _BBParserWorker As New BBParserWorker

    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim aThread As System.Threading.Thread

        aThread = New System.Threading.Thread(AddressOf _BBParserWorker.DoWork)
        aThread.Start()
    End Sub

    Protected Overrides Sub OnStop()
        _BBParserWorker.StopWork()
    End Sub

End Class

Public Class BBParserWorker

    Private _bMustStop As Boolean

    Private _thMain As System.Threading.Thread
    Private _rndGen As New Random(Now.Millisecond)

    Public Sub DoWork()
        Dim i As Integer = _rndGen.Next

        _thMain = System.Threading.Thread.CurrentThread
        _thMain.Name = "MainThread" & i.ToString

        While (Not _bMustStop)
            DoOperation()
            _thMain.Sleep(30000)
        End While
    End Sub

    Public Sub StopWork()
        _bMustStop = True
        If Not _thMain Is Nothing Then
            If Not _thMain.Join(1000) Then
                _thMain.Abort()
            End If
        End If
    End Sub

    Private Sub DoOperation()
        Dim fs As New FileStream("\\fileserver1\data\BBParser\log.txt", FileMode.OpenOrCreate, FileAccess.Write)
        Dim sw As New StreamWriter(fs)
        sw.BaseStream.Seek(0, SeekOrigin.End)
        sw.WriteLine("testing")
        sw.Flush()
        sw.Close()
        fs.Close()
    End Sub

End Class
Vaughn BighamSr. Software Engineer / Architect

Commented:
Also consider adding the the functionality for onPause() and onContinue()
In the designer set property CanPauseAndContinue of your service to True

Main Class:

    Protected Overrides Sub OnPause()
        _BBParserWorker.PauseWork()
        MyBase.OnPause()
    End Sub

    Protected Overrides Sub OnContinue()
        _BBParserWorker.ContinueWork()
        MyBase.OnContinue()
    End Sub

Worker Class:

    Public Sub PauseWork()
        _bMustStop = True
    End Sub

    Public Sub ContinueWork()
        DoWork()
    End Sub

Author

Commented:
Alll good ideas, but it still isn't working.  
Have you tried changing the account from LocalSystem to something that has access to that fileserver such as your ID or a generic domain ID?
Commented:
I did.  I guess you can say I solved this problem, but not by addressing the problem at hand.  Instead of the service doing the processing I needed it to do, I called another process every time the timer elapsed.  For some reason, my service didn't like doing File I/O along with database calls.  However, giving these duties to a second application that gets run by my service seemed to fix the issue.  Thanks!