Windows Service - finish up before stopping

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?

Who is Participating?
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
chladeAuthor Commented:
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?
chladeAuthor Commented:
I think that link may have an example that helps me.  Let me try it and I'll let you know.  Thanks.
chladeAuthor Commented:
That helped.  I figured it out.  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.