I'm doing this test on my machine. Obviously cpu % will vary, but I'm more interested in understanding what's going on. I simply create a new, blank, Console .Net Framework application (--not-- .net core). Here is the source of "Program.cs":
static void Main(string[] args)
{
myClass myClass = new myClass();
myClass.myInifiniteMethodAsync();
Console.WriteLine("Launched...");
Console.ReadLine();
}
Then this is the source of myClass:
class myClass
{
public async Task myInifiniteMethodAsync()
{
await Task.Run(() => myInfiniteMethod());
}
public void myInfiniteMethod()
{
//do some things but keep this thread holded...
//bool keepRunning = true;
//while (keepRunning) { } <--- this one takes 30% cpu...
Console.ReadLine(); // <--- this one takes 5% cpu...
}
}
I need the IfiniteMethod to stay always there, "holding" the thread forever. If I use the "while(true)" method, the CPU raises up to 30%. If I use the Console.ReadLine() method, the CPU stays around 5%.
I would like to understand why, and if there's a better method to hold a thread.