• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

windows service not running

please help im having some problems running this code as a windows service? It code workd fine as a applicationbut not as a service?

i have tryed degugging it an attached bugging to the process an it nerver hits the break point which i put on the "If File.Exists(ipPath) Then" line ?

But i now the service is started because it hits MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Information) line and puts in the Event log.....

any help will be apreciated...

Imports System.ServiceProcess
Imports System.IO
Imports System.Data

Public Class blackIp
    Inherits System.ServiceProcess.ServiceBase
    Dim MyLog As New EventLog

    Private ipPath As String ' = "c:\inetpub\wwwroot\w3.ip.com\current\theip.txt"
    Private fPath As String ' = "C:\Program Files\programe\test.ini"

#Region " Component Designer generated code "

    Public Sub New()

        ' This call is required by the Component Designer.

        ' Add any initialization after the InitializeComponent() call

    End Sub

    'UserService overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    ' The main entry point for the process
    <MTAThread()> _
    Shared Sub Main()
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        ServicesToRun = New System.ServiceProcess.ServiceBase() {New blackIp}

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  
    ' Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 10000
        Me.ServiceName = "blackIp"

    End Sub

#End Region

    Protected Overrides Sub OnStart(ByVal args() As String)

        If Not MyLog.SourceExists("blackIp") Then
            MyLog.CreateEventSource("blackIp", "blackIp Log")
        End If
        MyLog.Source = "blackIp"

            Dim ds = New DataSet
            ds.ReadXml(System.Windows.Forms.Application.StartupPath & "\settings.xml")
            ipPath = ds.Tables(0).Rows(0).Item("value")
            fPath = ds.Tables(0).Rows(1).Item("value")
        Catch ex As Exception
            MyLog.WriteEntry("blackIp Log", "Error: " & ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Information)
        End Try

        MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Information)
        Timer1.Enabled = True
    End Sub

    Protected Overrides Sub OnStop()
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If File.Exists(ipPath) Then
            Dim strContents As String
            Dim objReader As StreamReader

                objReader = New StreamReader(ipPath)
                strContents = objReader.ReadLine

                '--add to black ice settings

                '--Delete file

                MyLog.WriteEntry("blackIp Log", "IpChanged: " & strContents & " - " & CStr(TimeOfDay), EventLogEntryType.Information)
            Catch Ex As Exception
                MyLog.WriteEntry("blackIp Log", "Error: " & Ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Information)
            End Try
        End If
    End Sub

    Public Function SaveTextToFile(ByVal newIp As String) As String
        Dim Contents, strData As String
        Dim bAns As String = "False"
        Dim objWriter As StreamWriter
        Dim objReader As StreamReader


            '--read the file and change the currentIp line
            objReader = New StreamReader(fPath)
            Dim lines As Array = Split(objReader.ReadToEnd(), vbCrLf)

            Dim i As Integer
            For i = 0 To lines.GetUpperBound(0)
                If InStr(1, lines(i), "[MANUAL IP ACCEPT]") Then
                    'ACCEPT,,CurrentIp, 2004-11-30 12:22:47, 2004-12-01 12:22:47, 2000, BIgui
                    strData &= lines(i) & vbCrLf
                    strData &= "ACCEPT, " & newIp & ", CurrentIp, " & GetMySQLDate() & ", " & GetMySQLDatePlus() & ", 2000, BIgui" & vbCrLf
                ElseIf InStr(1, lines(i), "CurrentIp") Then
                    strData &= lines(i) & vbCrLf
                End If

            '--write to the file
            objWriter = New StreamWriter(fPath)
            bAns = True

        Catch Ex As Exception
            bAns = Ex.Message
        End Try
        Return bAns
    End Function

    Public Function GetMySQLDate() As String
        Dim dtNow As DateTime = DateTime.Now
        GetMySQLDate = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
    End Function

    Public Function GetMySQLDatePlus() As String
        Dim dtNow As DateTime = DateTime.Now
        GetMySQLDatePlus = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day + 1) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
    End Function

    Public Function PadZero(ByVal val) As String
        If Len(CStr(val)) = 1 Then
            PadZero = "0" & CStr(val)
            PadZero = val
        End If
    End Function

End Class
  • 2
1 Solution
Your using a Windows Forms timer within a service.  You should be using System.Timers timer.

Remove the timer you currently have & drag a new one from the Components tab of the toolbox onto your service, the event for this type of timer that you'll want to watch is its elapsed event rather than the tick event.

Fahad MukhtarDistinguished EngineerCommented:
this may not be a timer selection error... see the EVENTLOG and find out the error
mk_bAuthor Commented:
Thanks Andy, spot on....

Your welcome. Been there, done that when I first started out on windows services.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now