Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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?

Thanks,
Chris
0
chlade
Asked:
chlade
  • 3
  • 2
1 Solution
 
appariCommented:
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
0
 
appariCommented:
0
 
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
        Next

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now