Question

I have to code a Windows service, but I'm not sure about how to implement the stop of the service.

OnStop's documentation does not specify if I should let the ServiceBase instance in a consistent state or it's equivalent to a Dispose: will a new instance of the service be initialized on restart (like most unix services) or the same instance will be used?

Was it helpful?

Solution

A Windows service is basically a background process. Once it exits, all of the memory and system resources associated with the process are released. Restarting the service initializes a brand new instance.

If your service has nothing that strictly requires cleaning up, the OnStop method can be left blank. However, it is a good place to perform orderly shutdowns of things that need it, e.g., closing files, closing database connections, terminating socket connections, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top