cpu占用率为正弦曲线(C#实现)基本照抄书上的C++

internal值根据配置调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 100;
double[] busySpan = new double [COUNT]; //array of busy times
double[] idleSpan = new double [COUNT]; //array of idle times
int half = INTERVAL / 2;
double radian = 0.0;
for (int i = 0; i < COUNT; i++)
{
busySpan[i] = (double) (half + (Math.Sin(PI * radian) * half));
idleSpan[i] = INTERVAL - busySpan[i];
radian += SPLIT;
}

double startTime = 0;
int j = 0;
while (true)
{
j = j % COUNT;
startTime = Environment.TickCount;
while ((Environment.TickCount - startTime) <= busySpan[j]) ;
System.Threading.Thread.Sleep((int) idleSpan[j]);
j++;
}
}
}
}