windows service not running

Posted on 2004-12-01
Last Modified: 2012-06-27
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\\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
Question by:mk_b
    LVL 10

    Accepted 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.

    LVL 19

    Expert Comment

    this may not be a timer selection error... see the EVENTLOG and find out the error
    LVL 5

    Author Comment

    Thanks Andy, spot on....

    LVL 10

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now