Windows service not working

Greg.NET

Active member
Joined
Jun 4, 2007
Messages
27
Programming Experience
1-3
Its a simple application. At evert timer tick a folder is checked and its contents are processed. I had to convert this app to a service because it needs to run without me being logged in. It worked fine as a console app but when I converted it to a service it simply doesnt work. I installed it using installutil and then I started it in computer management.

I have no idea how to debug it. heres the code:

VB.NET:
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Service1
    Dim docId As String
    Dim customerid As String
    Dim ims As String
    Dim docClass As String
    Dim formatType As String


    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.
        Me.Timer1.Start()

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        Me.Timer1.Stop()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        scanfolder()
    End Sub

    Private Sub scanfolder()
        Dim strFileSize As String = ""
        Dim di As New DirectoryInfo(getDirPath(Directory.GetCurrentDirectory, "..\..\CTL"))
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.ctl")
        Dim fi As IO.FileInfo

        For Each fi In aryFi
            parseCTLfile(fi.Name, fi.FullName)
        Next
    End Sub

    Private Function getDirPath(ByVal startPath As String, ByVal endPath As String) As String
        Dim n As Integer = New Regex("\.\.\\").Matches(endPath).Count
        ' Dim regexCustomer As New Regex("(.*)(?=(\\[\w\s\.]+){" & n & "}$)")
        Dim regexCustomer As New Regex("(.*)(?=(\\[^\\]+){" & n & "}$)")
        Dim imsMatch As Match = regexCustomer.Match(startPath)

        If imsMatch.Success Then
            getDirPath = imsMatch.Value & endPath.Substring(endPath.LastIndexOf("\"))
        Else
            getDirPath = ""
        End If
    End Function

    Private Sub parseCTLfile(ByVal fullname As String, ByVal fullpath As String)
        Dim name As String = fullname.Substring(0, fullname.Length - 4)
        Dim reader As StreamReader = New StreamReader(fullpath)

        reader.BaseStream.Seek(0, SeekOrigin.Begin)

        Dim regexCustomer As New Regex("(?<=ClientID=)[0-9]+")
        Dim regexDoc As New Regex("(?<=FileNetDocID=)[0-9]+")
        Dim regexIMS As New Regex("(?<=FileNetIMS=)\w+")
        Dim regexdocClass As New Regex("(?<=DocClass=)\w+")
        Dim regexformatType As New Regex("(?<=FormatType=)\w+")

        While reader.Peek() > -1
            Dim line As String = reader.ReadLine()

            Dim customerMatch As Match = regexCustomer.Match(line)
            Dim docMatch As Match = regexDoc.Match(line)
            Dim imsMatch As Match = regexIMS.Match(line)
            Dim docClassMatch As Match = regexdocClass.Match(line)
            Dim formatTypeMatch As Match = regexformatType.Match(line)

            If customerMatch.Success Then
                customerid = customerMatch.Value.ToString
            ElseIf docMatch.Success Then
                docId = docMatch.Value.ToString
            ElseIf imsMatch.Success Then
                ims = imsMatch.Value.ToString
            ElseIf docClassMatch.Success Then
                docClass = docClassMatch.Value.ToString
            ElseIf formatTypeMatch.Success Then
                formatType = formatTypeMatch.Value.ToString
            End If
        End While
        reader.Close()

        If (Not docId Is Nothing) And (Not customerid Is Nothing) And (Not ims Is Nothing) And (Not docClass Is Nothing) And (Not formatType Is Nothing) Then
            If updateRecord(name) < 1 Then
                insertDocument(name)

                docId = Nothing
                customerid = Nothing
                ims = Nothing
                docClass = Nothing
                formatType = Nothing
            End If

        End If
        File.Move(fullpath, getDirPath(fullpath, "..\..\CTL -  Archive") & "\" & fullname)
    End Sub

    Private Sub insertDocument(ByVal name As String)
        Dim comm As New SqlClient.SqlCommand()
        comm.Connection = New SqlClient.SqlConnection("Password=y33f_sql;User ID=PDIGROUP;Initial Catalog=PDIGroup1;server=crprchmsq21e")
        comm.CommandType = CommandType.Text
        comm.CommandText = "Insert into insSaleToolTblFileNetDocs (docID, customerID, ims, dateCreated, filename, docClass, formatType) Values('" & docId & "','" & customerid & "','" & ims & "',GETDATE(),'" & name & "','" & docClass & "','" & formatType & "')"
        comm.Connection.Open()
        comm.ExecuteNonQuery()
        comm.Connection.Close()
        comm.Dispose()
    End Sub

    Private Function updateRecord(ByVal name As String) As Integer
        Dim comm As New SqlClient.SqlCommand()
        comm.Connection = New SqlClient.SqlConnection("Password=y33f_sql;User ID=PDIGROUP;Initial Catalog=PDIGroup1;server=crprchmsq21e")
        comm.CommandType = CommandType.Text
        comm.CommandText = "Update insSaleToolTblFileNetDocs set docid = '" & docId & "', ims = '" & ims & "', docClass = '" & docClass & "', formatType = '" & formatType & "', datecreated = GetDate() where filename ='" & name & "'"
        comm.Connection.Open()
        Dim rowseffected As Integer = comm.ExecuteNonQuery()
        comm.Connection.Close()
        comm.Dispose()
        updateRecord = rowseffected
    End Function
End Class
 
i figured out how to debug it. It actually works when I step through it in debug mode! This is very frustrating. How could it work when debugging but not when I start it in computer management?
 
ok I replaced the timer I drag and dropped from the toolbar with a "system.timers.timer" and everything works now. You know, you would think that the timer from the toolbar would work since its on the freaking toolbar. what a headache.
 
The Windows.Forms timer must be used in a window ("requires that the user code have a UI message pump available"), which Windows Service don't have. Great you found the server timer.
 
Back
Top