Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

c# - Reduce CPU utilization when condition is false

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...