• 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()
        MyBase.New()

        ' This call is required by the Component Designer.
        InitializeComponent()

        ' 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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    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)
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 10000
        '
        'blackIp
        '
        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"

        Try
            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

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

                '--add to black ice settings
                SaveTextToFile(strContents)

                '--Delete file
                File.Delete(ipPath)

                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

        Try

            '--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, 111.111.111.111,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
                Else
                    strData &= lines(i) & vbCrLf
                End If
            Next
            objReader.Close()

            '--write to the file
            objWriter = New StreamWriter(fPath)
            objWriter.Write(strData)
            objWriter.Close()
            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)
        Else
            PadZero = val
        End If
    End Function

End Class
0
mk_b
Asked:
mk_b
  • 2
1 Solution
 
heintalusCommented:
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.

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


../mk
0
 
heintalusCommented:
Your welcome. Been there, done that when I first started out on windows services.
0

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