|
-
Jul 29th, 2004, 09:21 AM
#1
[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:
Imports System.ServiceProcess
Public Class FunkehMunkehService
Inherits System.ServiceProcess.ServiceBase
#Region "Asynchronous execution delegates"
Public Delegate Sub OnStartDelegate()
Public Delegate Sub OnStopDelegate()
#End Region
#Region "ServiceProcess.ServiceBase overrides"
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim OnStartDelegateInst As OnStartDelegate = AddressOf OnStartAsynch
OnStartDelegateInst.BeginInvoke(AddressOf OnStopCallback, OnStartDelegateInst)
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Dim OnStopDelegateInst As OnStopDelegate = AddressOf OnStopAsynch
OnStopDelegateInst.BeginInvoke(AddressOf OnStopCallback, OnStopDelegateInst)
End Sub
#End Region
#Region "Asynchronous start/stop handling"
Private Sub OnStartAsynch()
'\\ Do the real start code here
End Sub
Private Sub OnStopAsynch()
'\\ Do the real stop code here
End Sub
Private Sub OnStartCallback(ByVal ar As IAsyncResult)
ar.AsyncState.EndInvoke(ar)
End Sub
Private Sub OnStopCallback(ByVal ar As IAsyncResult)
ar.AsyncState.EndInvoke(ar)
End Sub
#End Region
End Class
-
Jul 29th, 2004, 09:22 AM
#2
If you only knew how much you just helped me with that!
-
Jul 29th, 2004, 09:37 AM
#3
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|