Mono application using Console.CancelKeyPress cannot be started in the background

I have a console application in Mono on Linux that uses Console.CancelKeyPress to listen on SIGINT . However, these applications refuse to run in the background, as they always stop immediately.

Here is an example:

 using System; using System.Threading; static class Program { static void Main() { Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); Console.WriteLine("Sleeping"); Thread.Sleep(100000); Console.WriteLine("Done"); } static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { Console.WriteLine("In handler."); } } 

If I try to run this application in the background in bash, this is the result:

 ~/test$ mono test.exe & [1] 4516 ~/test$ [1]+ Stopped mono test.exe ~/test$ 

I had to press Enter to see the message “stopped”, but this happens immediately. As you can see, it never reaches a call to Console.WriteLine .

If I try to send SIGINT using kill (which should terminate the process because the handler does not set e.Cancel to true ), nothing happens until I resume the process using fg , which indicates that it ends immediately (it never not included in the handler).

If I run the process in the foreground and then abort it with ctrl-z and use bg , it will stop immediately again, only this time if I send SIGINT and then resume it with fg , the handler calls.

If I remove the Console.CancelKeyPress event handler assignment, the process runs in the background just fine.

I am using Mono 2.10.8 and Debian 6.0.2.

If all else fails, I can replicate the functionality that I need using Mono.Unix.UnixSignal , but if anyone has a solution for this, I would really like to hear it.

UPDATE: There seems to be another problem with Console.CancelKeyPress for my purposes. I started my process using nohup , so the input is not a console, but / dev / null. In this situation, Mono never fires the CancelKeyPress event, even if you send SIGINT with kill . UnixSignal is that.

+4
source share

Source: https://habr.com/ru/post/1397747/


All Articles