The code used to control the CPU usage through C ++ in the previous section to display the sine curve is rewritten with C #, which is better.
Connection in the previous section: http://www.bkjia.com/kf/201110/107759.html
C # code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Diagnostics;
Namespace cpu_4_2_csharp
{
Class Program
{
Static void Main (string [] args)
{
Const int SAMPLING_COUNT = 200;
Const double PI = 3.14159;
Const int total_amplece = 250; // the length of each time piece
Double [] busySpan = new double [SAMPLING_COUNT];
Int ampl_= TOTAL_AMPLITUDE/2;
Double radian = 0.0;
Double radianIncreament = 2.0/(double) SAMPLING_COUNT;
For (int I = 0; I <SAMPLING_COUNT; I ++)
{
BusySpan [I] = (double) (amplitude + Math. Sin (PI * radian) * amplian );
Radian + = radianIncreament;
}
Int startTick = Environment. TickCount;
For (int j = 0; j = (j + 1) % SAMPLING_COUNT)
{
StartTick = Environment. TickCount;
While (Environment. TickCount-startTick) <busySpan [j])
{
//
}
System. Threading. Thread. Sleep (total_ampl--(int) busySpan [j]);
}
}
}
}
:
1. The time slice is 300
2. The time slice is 250
3. The time slice is 200
From: Watkins. Song