简体   繁体   English

如何退出单声道服务?

[英]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. 这当然很棒,因为我的代码中的Console.WriteLine("...")会写入屏幕。

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. 我接下来要做的是在OnStart中添加新线程,因为当然代码(服务)永远不会被认为是开始的。

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. 使用--debug标志时,控制台会写入工作,但它不会退出。 I can only issue a kill -9 to end the application. 我只能发出kill -9来结束应用程序。 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. 花了一段时间,但我终于找到了:Console.IsOutputRedirected。 (Part of .Net 4.5) (.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 这样,当使用--debug开关测试应用程序时,它将响应/结束Ctrl + c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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