Stopping a Windows Service in a Timely Manner...
-
Hello! I recently started writing my first Windows Service project in C#, and I'm at a point now where I have a lot of time to perfect things before I continue. I was wondering what's considered generally "good practice" with regards to Windows Services. Specifically, the stopping mechanism. When my service starts, it kicks off a few threads that perform some file manipulation. When the service stops, I want it to shut those threads down gracefully. Ideally, shutting down the service puts the file system in a state where the manipulation can be easily resumed upon restarting the service. So, when shutting down the service, I'd prefer to let one of the threads (if it's currently doing work) finish the works it's already started, but in some cases, this can take several minutes. Is this considered bad practice, or is this something I shouldn't really be concerned with? Are there other things anyone thinks I should consider?
-
Hello! I recently started writing my first Windows Service project in C#, and I'm at a point now where I have a lot of time to perfect things before I continue. I was wondering what's considered generally "good practice" with regards to Windows Services. Specifically, the stopping mechanism. When my service starts, it kicks off a few threads that perform some file manipulation. When the service stops, I want it to shut those threads down gracefully. Ideally, shutting down the service puts the file system in a state where the manipulation can be easily resumed upon restarting the service. So, when shutting down the service, I'd prefer to let one of the threads (if it's currently doing work) finish the works it's already started, but in some cases, this can take several minutes. Is this considered bad practice, or is this something I shouldn't really be concerned with? Are there other things anyone thinks I should consider?
It sounds OK to me, though "several minutes" sounds excessive. Is there a way to have them stop after the current operation even though there are still other things to do?
agent00zelda wrote:
it kicks off a few threads that perform some file manipulation
Can those be in their own Services?
-
It sounds OK to me, though "several minutes" sounds excessive. Is there a way to have them stop after the current operation even though there are still other things to do?
agent00zelda wrote:
it kicks off a few threads that perform some file manipulation
Can those be in their own Services?
Thank you for your response!!
PIEBALDconsult wrote:
Can those be in their own Services?
Actually, yes, and they are.. I guess I should have mentioned that part :) The way I had it implemented before I made the initial post was I pretty much let it finish everything until it was able to submit the data to the other services. But Windows spazzed out because it couldn't stop the main service until it finished the preceding operations. What I ended up doing was defining like a project "state" DataTable that could be exported and imported again when the service restarts. Your suggestion was well-taken -- pretty much made the current operation finish and then updated the project state based on that. I don't know if using a DataTable is the best approach, but it seems to work much better, since it takes considerably less time to stop the service. Still open to suggestions if you or anyone can think of a better approach, though!! I guess I'm in a C# experimentation phase :)