using System;
using System.Threading;
using System.Diagnostics;
public sealed class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Program SIN");
Console.WriteLine("Making the Windows Task Manager show Sine Wave Pattern in CPU usage");
Console.WriteLine("Now look at your task manager cpu usage!");
int numOfCPUs = Environment.ProcessorCount;
for(int i = 0; i < numOfCPUs; ++i)
{
ThreadPool.QueueUserWorkItem(SinWave);
}
Thread.Sleep(Timeout.Infinite);
}
private static void SinWave(object dummy)
{
while(true)
{
for(double i = 0.0; i < 2 * Math.PI; i += 0.1)
{
Compute(500, 0.2); // 修改这里的数值控制CPU使用率
}
}
}
private static Stopwatch m_sw = new Stopwatch();
private static void Compute(long time, double percent)
{
long runTime = (long)(time * percent);
long sleepTime = time - runTime;
m_sw.Start();
while(m_sw.ElapsedMilliseconds - runTime < 0 )
{
// Spin the CPU. Just doing nothing is OK
}
m_sw.Stop();
m_sw.Reset();
Thread.Sleep((int)sleepTime);
}
}