JPelletier
JPelletier

Reputation: 2109

Mono console app as daemon take 100% cpu

I have a console app (I know it should be a service) with a loop at the end so it never stops:

while (true)
{
    Console.ReadLine();
}

It run perfectly on a Raspberry Pi using mono (raspbian wheezy) but since I upgraded to the Pi 2 with Raspbian Jessie, it use 100% cpu when started as a Daemon.

It seems that Readline() is always returning an empty line. Any idea? My init.d script is really simple for testing purpose:

cd /myAppDir/
mono MyApp.exe &
exit 0

I tried with nohup, with/without the & at the end, without success

[Edit] I can't use Thread.Sleep since I have a Timer pooling the UART. An easy solution would be to create another thread for all the work and just do the Sleep in the main thread but I would like to understand why it doesn't work like this

Upvotes: 0

Views: 812

Answers (2)

Derek Menendez
Derek Menendez

Reputation: 27

You only need this before finish Main(string[] args)

private static void Main(string[] args)
{
     //call method for daemon before while
     while (true)
     {
          Thread.Sleep(1000);
     }
}

with this your app not will take 100% CPU usage

Upvotes: 0

Gooseman
Gooseman

Reputation: 2231

it uses 100% cpu when started as a Daemon.

I do not know how are you starting your process as a daemon but typically, daemon processes redirect stdin to /dev/null.

Null device yields EOF immediately, so Console.ReadLine() will never block.

I guess (I have no Raspbian), the way of running daemons changed from Raspbian Wheezy to Jessie.

Upvotes: 2

Related Questions