what is the right way to exit Windoes Service OnStart if configuration is wrong and nothing to do in

Posted by matti on Stack Overflow See other posts from Stack Overflow or by matti
Published on 2010-04-21T07:29:02Z Indexed on 2010/04/21 7:33 UTC
Read the original article Hit count: 335

Filed under:
|
|

Is something like this ok?

protected override void OnStart(string[] args)
{
    if (SomeApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
}

protected override void OnStop()
{
    SomeApp.TearDown();
    base.OnStop();
}

Here Initialize reads a config file and if it's wrong there's nothing to do so service should STOP! If config is ok StartMonitorAndWork starts:

Timer(new TimerCallback(DoWork), null, startTime, loopTime);

and DoWork polls database periodically.

The question is: "Is exiting OnStart without doing nothing enough if Initialize returns false?

OR should there be something like this:

private void ExitService()
{
    this.OnStop();
    System.Environment.Exit(1);
}

protected override void OnStart(string[] args)
{
    if (ObjectFolderApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
    else
    {
        ExitService();
    }
}

Thanks & BR - Matti

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-services