Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 6, 2011 6:55 AM
Hi,
I'm getting an error while stopping the windows service developed in C#. It says:
Could not stop the Service1 service on local computer
Error 1067: The process terminated unexpectedly.
But still the service is getting stopped.
Can anybody suggest how it can be avoided?
Thanks
All replies (6)
Wednesday, September 7, 2011 1:16 AM ✅Answered | 1 vote
Don't do this. When Service Controller see the process return non-zero return code, it'll treat that as signal of something abnormal happened.
Also, you're encouraged to do proper cleanup (e.g.: stop all the timers, send abort to all worker threads and wait them to return, etc.) instead of relying .NET runtime to take care of these for you, especially if your service somehow used some unmanaged resource that the runtime has no idea of.
Wednesday, September 7, 2011 4:19 AM ✅Answered
I agree with what @Cheong00 has said. You should avoid using Environment.Exit(-1) call in your OnStop() method. BTW, what is the reason behind calling it?--Abhimanyu
Click the 'Vote as Helpful' arrow if this post was helpful.
Tuesday, September 6, 2011 8:29 AM
There may be exception thrown during service stop. Show us your OnStop() method.
Also, you may want to use a try...catch... block to log what happened during service stop.
Tuesday, September 6, 2011 9:03 AM
Hi
Did you went through EventViewer for error information?
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
Tuesday, September 6, 2011 9:27 AM
OnStop() Method has only one statement
Environment
.Exit(-1);
and I have placed it in try-catch block.
But still it is throwing error in a message box when I manually stop the service in the services.msc window.
Tuesday, September 6, 2011 9:32 AM
EventViewer has errors logged while stopping the service. It says:
The Service1 service terminated unexpectedly. It has done this 1 time(s).
Source being Service Control Manager.