任意調整CUP成直綫

        //任意調整CUP直綫
        static void getUsage(float level)
        {
            PerformanceCounter p = new PerformanceCounter("Processor", "% Processor Time", "_Total");  
            if (p == null)
            {
                return;
            }

            while (true)
            {
                if (p.NextValue() > level)
                {
                    System.Threading.Thread.Sleep(20);
                }
            }

        }

 PS.程序在本機測試顯示50%。

        /// <summary>
        ///
        /// </summary>
        /// <param name="cupHz">cup HZ,如:2.53</param>
        /// <param name="ratio">cup 占用百分比,如:0.8</param>
        static void getUsage_1(float cupHz, float ratio)
        {
            double r = (1 - ratio) * 10;
            double hz = (cupHz * Math.Pow(10d, 9d) * 2) / r;
            for (; ; )
            {
                for (int i = 0; i < hz / 1000; i++)
                {

                }

                System.Threading.Thread.Sleep(15);
            }
        }
PS.  對0.5(50%)以上較准.  需知道本地CUP工作頻率.
 

原文地址:https://www.cnblogs.com/wang123/p/1372821.html