Windows Service - finish up before stopping

Posted on 2007-08-06
Last Modified: 2010-05-18
I have a Windows Service that processes transactions that are dropped in a queue.  I want to ensure that if the administrator stops the Service, that it will finish up the current transaction first.  How can I do this?

Question by:chlade
    LVL 39

    Expert Comment

    you can write your code in onstop method in your service and do all the cleanup there

    Protected Overrides Sub OnStop()
    ' write the code here to finish up the current transaction.
    End Sub
    LVL 39

    Accepted Solution


    Author Comment

    I'm not sure what you mean.  It's not a matter of doing a few things before closing down (such as closing connections).

    Imagine that I have this code in my service:

            For i As Integer = 1 To 10
                Dim objEventLog As New EventLog()
                objEventLog.Source = "Application"
                objEventLog.WriteEntry("Test " & i.ToString, EventLogEntryType.SuccessAudit, 0)
                System.Threading.Thread.Sleep(5000) 'Slow it down

    If I stop the service while the program is in the middle of this loop, it will stop without finishing the loop.  I want it to finish the loop and then end.  Is this possible?

    Author Comment

    I think that link may have an example that helps me.  Let me try it and I'll let you know.  Thanks.

    Author Comment

    That helped.  I figured it out.  Thanks!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now