Results 1 to 3 of 3

Thread: [Tip] Writing a service? Handle OnStart and OnStop asynchronously

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    [Tip] Writing a service? Handle OnStart and OnStop asynchronously

    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

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    If you only knew how much you just helped me with that!

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Cool, thanks for the tip! In one of my services my OnStart method does a lot of stuff (I thought it was 30 seconds), so my work-around was to launch a thread to take care of it. That way it looks like the service starts super-quick.

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