I'm developing an application that runs every day in a particular time, I used while loop inside when inside while loop condition is false CPU take much time and resource to process, how I reduce CPU utilization when inside the loop condition false.
while (true) // run everyday without stop
{
IntPtr hwnd; // hide command prompt file
hwnd = GetConsoleWindow(); //
ShowWindow(hwnd, SW_HIDE); //
TimeSpan startMor= new TimeSpan(int.Parse(SHP1), int.Parse(SMP1), 0); // set the hours and minutes
TimeSpan endMor = new TimeSpan(int.Parse(EHP1), int.Parse(EMP1), 0); //
TimeSpan startEve = new TimeSpan(int.Parse(SHP2), int.Parse(SMP2), 0); //
TimeSpan endEve = new TimeSpan(int.Parse(EHP2), int.Parse(EMP2), 0); //
TimeSpan now = DateTime.Now.TimeOfDay; // current time of day
var hour = DateTime.Now.Hour; // current hours, set to check
var minutes = DateTime.Now.Minute; // current minutes, set to check
// hour to minutes conversion
int minutesProcess1 = ((int.Parse(EHP1) - (int)hour) * 60) + int.Parse(EMP1) - (int)minutes; // process 1 conversion
int minutesProcess2 = ((int.Parse(EHP2) - (int)hour) * 60) + int.Parse(EMP2) - (int)minutes; // process 2 conversion
//int time_to_process = 1;
if ((now >= startMor) && (now < endMor))
{
// var process1 = Process.Start("notepad.exe");
var process1 = Process.Start(path_1);
Thread.Sleep(1000 * 60 * 1 * minutesProcess1);
//process.WaitForExit(10000);
process1.Kill();
}
else if((now >= startEve) && (now < endEve))
{
var process2 = Process.Start(path_2);
// var process2 = Process.Start("notepad.exe");
Thread.Sleep(1000 * 60 * 1 * minutesProcess2);
//process.WaitForExit(1000);
process2.Kill();
}
}
question from:
https://stackoverflow.com/questions/65840230/reduce-cpu-utilization-when-condition-is-false 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…