Simple .exe using 50% of the processor using System.Threading

I have a very simple executable file that checks a specific folder every 3 seconds to see if the file (s) are there. If he finds the file (s), he does something, and then returns to the folder check every 3 seconds.

People report that sometimes this executable file occupies 50% of its processor, are there any suggestions on how to do this correctly. Below is a sample code of how I do this.

// Check our folder every x seconds
Timer = new System.Threading.Timer(TimerCallback, null, 0, Global.SecondsToCheckPrintFolder * 1000);
+3
source share
2 answers

You must use FileSystemWatcher.

To answer your question, your main thread is probably working while(true) { }, which will permanently lose the CPU.

, Application.Run().
Thread.Sleep(-1).

+9

, , . , ?

while(true)
{
    if (File.Exists(@"someFile"))
    {
         // Do stuff
    }
    Thread.Sleep(3000);
}
+1

All Articles