Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Code Library (http://www.xtremevbtalk.com/code-library/)
-   -   [Tip] Writing a service? Handle OnStart and OnStop asynchronously (http://www.xtremevbtalk.com/code-library/180465-tip-writing-service-handle-onstart-onstop-asynchronously.html)

Merrion 07-29-2004 09:22 AM

[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....

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



All times are GMT -6. The time now is 12:45 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.