Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Good morning everyone,

I have a service,and I was wondering if there is any method to do the service automatically start.For example: my service run 24 hours a day. Even with the method CanStop set to false, the service can sometimes occur fall, either for lack of energy,the terminal being rebooted, or any other problem.

The only way I checked so far,would be setting a method to restart the service from time to time, but isnt good for me that way because it will greatly influence the performance of the windows service.

Could someonegive me a hand?

Thanks to all

sry about my english. isnst good or pretty
Posted
Updated 1-Mar-12 7:51am
v2
Comments
Sergey Alexandrovich Kryukov 1-Mar-12 13:36pm    
What is: "raise a windows service".
Please look at the text: you made it unreadable -- many blank spaces were disappeared. Please fix it. See "Improve question" above. Please be more accurate, otherwise -- who would volunteer to read this?

Thanks for fixing improving the question.
--SA

1 solution

I hope you know that the services can be started automatically on system start-up, this is one of the standard options of the service installation which can be changed at any time using the Service Controller or services applet:
%windir%\system32\services.msc

So, if the host running the service is rebooted, the service will restart automatically.

What to do with other problems disrupting the service? I agree with you: the idea of regular restarting of a service from time to time would not work properly.

Well, you will really need to make your service fail-tolerant. Use the following principles:

Execute every logically distinct activity in a separate thread. Make sure you are not working with random or unpredictable number of threads. The best approach is creating a fixed number of threads in the very beginning and keeping them either working or in a wait state, waiting at some blocking call (like in case of networking) or a call to thread synchronization primitives. Is such state, the thread consumes zero CPU time until waken up by some thread synchronization event.

Catch all exceptions at the very top of each thread by wrapping it in the try-catch-finally block. Besides, devise an "infinite" outer execution loop where you can use a last chance to catch any exception and try to restart. Schematically, it should look like this:
C#
void ServiceThreadBody() { //something passed to the constructor of System.Threading.Thread.Thread
    while(!exitCondition) {
        try {
            MainThreadCycle();
        } catch (MySpecificException e) {
            ProcessSpecificException(e);
        } catch (MyOtherSpecificException e) {
            ProcessAnotherSpecificException(e);
        //... some other exception handling specific particular exception types you can foresee 
        } catch { //all other exceptions; not generally recommended but important in this case
            ProcessUnexpectedException();
        } finally {
            SomeUniversalCleanUp();
        } //exception
    } //main restart loop
} //ServiceThreadBody

void MainServiceCycle() {
    while (keepServicingCondition) {/* do the main job... */}
} //MainServiceCycle


When you handle an exception, you need to perform necessary clean up. One thing you would need to do is to log the exception information to the system log using the class System.Diagnostics.EventLog, please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Do something like that in each thread performing the service.

For some particular idea on fault tolerance, on the example of network service, please see my past answer:
Multple clients from same port Number[^].

Please see my past answers on advanced use of logging:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

Please see my past answers on some ideas on exception handling:
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Handling exceptions in class library (dll)[^].

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 1-Mar-12 15:39pm    
Very good reply Sergey!
Sergey Alexandrovich Kryukov 1-Mar-12 15:40pm    
Thank you, Espen.
--SA
El_Codero 1-Mar-12 19:18pm    
Very good explanation,5!
Sergey Alexandrovich Kryukov 1-Mar-12 19:42pm    
Thank you, Björn.
--SA
adrielmf 2-Mar-12 7:05am    
Very good explanation man. Thx, gonna try that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900