When you start or stop a service in Windows if the OnStart or OnStop handler doesn't return within 20 seconds the service is marked as not working.

To prevent this do your OnStart or OnStop processing asynchronously....

VB Code:
  1. Imports System.ServiceProcess
  2.  
  3. Public Class FunkehMunkehService
  4.     Inherits System.ServiceProcess.ServiceBase
  5.  
  6. #Region "Asynchronous execution delegates"
  7.     Public Delegate Sub OnStartDelegate()
  8.     Public Delegate Sub OnStopDelegate()
  9. #End Region
  10.  
  11. #Region "ServiceProcess.ServiceBase overrides"
  12.  
  13.     Protected Overrides Sub OnStart(ByVal args() As String)
  14.         ' Add code here to start your service. This method should set things
  15.         ' in motion so your service can do its work.
  16.         Dim OnStartDelegateInst As OnStartDelegate = AddressOf OnStartAsynch
  17.         OnStartDelegateInst.BeginInvoke(AddressOf OnStopCallback, OnStartDelegateInst)
  18.  
  19.     End Sub
  20.  
  21.     Protected Overrides Sub OnStop()
  22.         ' Add code here to perform any tear-down necessary to stop your service.
  23.         Dim OnStopDelegateInst As OnStopDelegate = AddressOf OnStopAsynch
  24.         OnStopDelegateInst.BeginInvoke(AddressOf OnStopCallback, OnStopDelegateInst)
  25.  
  26.     End Sub
  27.  
  28. #End Region
  29.  
  30. #Region "Asynchronous start/stop handling"
  31.     Private Sub OnStartAsynch()
  32.          '\\ Do the real start code here
  33.     End Sub
  34.  
  35.     Private Sub OnStopAsynch()
  36.         '\\ Do the real stop code here
  37.     End Sub
  38.  
  39.     Private Sub OnStartCallback(ByVal ar As IAsyncResult)
  40.         ar.AsyncState.EndInvoke(ar)
  41.     End Sub
  42.  
  43.     Private Sub OnStopCallback(ByVal ar As IAsyncResult)
  44.         ar.AsyncState.EndInvoke(ar)
  45.     End Sub
  46.  
  47. #End Region
  48.  
  49. End Class