Results 1 to 6 of 6

Thread: Debugging and windows services

Threaded View

  1. #1

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21

    Debugging and windows services

    Hey everybody, this is driving me nuts.

    I'm trying to write a windows service that just starts a timer and then does its magic evertime the timer ticks. I had everything written, but then I ran into a bunch of problems trying to get it installed. So I reduced it down to a very simple service that just writes info to an event log.

    The problem is when I try to start the service, it starts and stops right away, and never gets into the OnStart method (I put a message box in the OnStart and it never appears). I can't for the life of me figure out how to debug a process that isn't running. Is it possible to attach the debugger to a stoped service and have the debugger enter it when the service is attempted to be started?

    Or does anybody know of some stupid thing I'm missing in my program thats causing this error when I try to start the service:

    The <service name> on the local computer started and then stoped. Som services stop automatically if they have no work to do, for example the performance logs and alert service.
    My OnStart has work to do, so I dont know why it just quits. Below is the code for my service:

    VB Code:
    1. Imports System.ServiceProcess
    2.  
    3. Public Class SnapshotService
    4.     Inherits System.ServiceProcess.ServiceBase
    5.  
    6. #Region " Component Designer generated code "
    7.  
    8.     Public Sub New()
    9.         MyBase.New()
    10.  
    11.         ' This call is required by the Component Designer.
    12.         InitializeComponent()
    13.  
    14.         ' Add any initialization after the InitializeComponent() call
    15.  
    16.     End Sub
    17.  
    18.     'UserService overrides dispose to clean up the component list.
    19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    20.         If disposing Then
    21.             If Not (components Is Nothing) Then
    22.                 components.Dispose()
    23.             End If
    24.         End If
    25.         MyBase.Dispose(disposing)
    26.     End Sub
    27.  
    28.     ' The main entry point for the process
    29.     <MTAThread()> _
    30.     Shared Sub Main()
    31.         Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    32.  
    33.         ' More than one NT Service may run within the same process. To add
    34.         ' another service to this process, change the following line to
    35.         ' create a second service object. For example,
    36.         '
    37.         '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
    38.         '
    39.         ServicesToRun = New System.ServiceProcess.ServiceBase() {New SnapshotService()}
    40.  
    41.         System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    42.     End Sub
    43.  
    44.     'Required by the Component Designer
    45.     Private components As System.ComponentModel.IContainer
    46.  
    47.     ' NOTE: The following procedure is required by the Component Designer
    48.     ' It can be modified using the Component Designer.  
    49.     ' Do not modify it using the code editor.
    50.     Friend WithEvents LocalEventLog As System.Diagnostics.EventLog
    51.     Public WithEvents LocalTimer As System.Timers.Timer
    52.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    53.         Me.LocalTimer = New System.Timers.Timer()
    54.         Me.LocalEventLog = New System.Diagnostics.EventLog()
    55.         CType(Me.LocalTimer, System.ComponentModel.ISupportInitialize).BeginInit()
    56.         CType(Me.LocalEventLog, System.ComponentModel.ISupportInitialize).BeginInit()
    57.         '
    58.         'LocalTimer
    59.         '
    60.         Me.LocalTimer.Interval = 100000000000
    61.         '
    62.         'SnapshotService
    63.         '
    64.         Me.ServiceName = "Snapshot Service"
    65.         CType(Me.LocalTimer, System.ComponentModel.ISupportInitialize).EndInit()
    66.         CType(Me.LocalEventLog, System.ComponentModel.ISupportInitialize).EndInit()
    67.  
    68.     End Sub
    69.  
    70. #End Region
    71.  
    72.     Public Verbose As Boolean
    73.     Private DataFile As String
    74.     Private DataSchema As String
    75.  
    76.     Protected Overrides Sub OnStart(ByVal args() As String)
    77.         Windows.Forms.MessageBox.Show("Entered OnStart")
    78.         ' Add code here to start your service. This method should set things
    79.         ' in motion so your service can do its work.
    80.  
    81.         ' Create the event log if it doesn't exist, and set the local
    82.         ' event log to point to it
    83.         If Not EventLog.SourceExists("Snapshot") Then
    84.             EventLog.CreateEventSource("Snapshot", "Snapshot Log")
    85.         End If
    86.         LocalEventLog.Source = "Snapshot Service"
    87.         LocalEventLog.Log = "Snapshot"
    88.  
    89.         LocalEventLog.WriteEntry("The Snapshot service is starting")
    90.  
    91.         ' Get the bits from the registry so we know where to start looking
    92.         DataFile = GetSetting("Snapshot", "", "DataFile", Nothing)
    93.         DataFile = GetSetting("Snapshot", "", "DataSchema", Nothing)
    94.  
    95.         ' Check that all the required bits and pieces are about
    96.         If DataFile = Nothing Then _
    97.             Message_Die("There is no service data. You must create a DataFile before Snapshot can run.")
    98.         If Not (System.IO.File.Exists(DataFile)) Then _
    99.             Message_Die("'" & DataFile & "' does not exist, you must create the file before snapshot can run.")
    100.  
    101.         If DataSchema = Nothing Then Message_Die("There is no service data. You must create a DataSchema before Snapshot can run.")
    102.         If Not (System.IO.File.Exists(DataSchema)) Then _  
    103.             Message_Die("'" & DataSchema & "' does not exist, you must create the file before snapshot can run.")
    104.  
    105.         ' Set the timer to fire on the next hour
    106.         Dim TimeTillHour As Integer = (60 - Minute(Now())) * 60000
    107.         LocalTimer.Interval = TimeTillHour
    108.         LocalTimer.Enabled = True
    109.  
    110.         LocalEventLog.WriteEntry("The Snapshot service started successfully")
    111.         Windows.Forms.MessageBox.Show("Exiting OnStart")
    112.     End Sub
    113.  
    114.     Protected Overrides Sub OnStop()
    115.         Windows.Forms.MessageBox.Show("Entered OnStop")
    116.         ' Add code here to perform any tear-down necessary to stop your service.
    117.         LocalEventLog.WriteEntry("The Snapshot service was stoped")
    118.         Windows.Forms.MessageBox.Show("Exiting OnStop")
    119.     End Sub
    120.  
    121.     Private Sub Message_Die(ByVal Message As String)
    122.         LocalEventLog.WriteEntry(Message, EventLogEntryType.Error)
    123.         Me.Dispose()
    124.     End Sub
    125. End Class
    Last edited by kylek; Dec 22nd, 2003 at 06:01 PM.
    INDEMNITY a legal exemption from liability for damages.
    VBForum Name: kylek
    BLOG | FORUMMARK | VENTURE CREW | DORMLIFE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width