简体   繁体   中英

How to exit a Mono-service?

I'm trying to exit my mono-service cleanly.

I'm building a mono-service, and to make things easy to debug, I am running it as:

mono-service --debug /etc/App.exe

This is great of course because the Console.WriteLine("...") I have in my code writes out to the screen.

The next thing I do is add the new thread in the OnStart, because of course then the code (service) would never be considered started.

protected override void OnStart(string[] args)
{
    Thread serviceThread = new Thread(new ThreadStart(StartMyApplicationAsync));
    serviceThread.Start();
}

While with the --debug flag, the console writes work, but it does not exit, ever. I can only issue a kill -9 to end the application. The service cannot even be restarted.

How can I exit my mono-service cleanly once I have started a thread?

It took a while, but I finally found: Console.IsOutputRedirected. (Part of .Net 4.5)

If anyone else find this post, the answer would be:

protected override void OnStart(string[] args)
{
    if(Console.IsOutputRedirected)
    {
        Thread serviceThread = new Thread(new ThreadStart(StartMyApplicationAsync));
        serviceThread.Start();
    } else {
        StartMyApplicationAsync();
    }
}

This way when testing the applications with the --debug switch, it will respond/end to Ctrl+c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM