看了《编程之美》的文章后的实现
cpu时间呈正弦曲线的代码
在自己的机器上是画出来了。
cpu时间呈正弦曲线的代码
- using System;
- using System.Threading;
- using System.Runtime.InteropServices;
- class Program
- {
- [DllImport("Kernel32.Dll")]
- extern static int SetProcessPriorityBoost(int ProcPtr, int mask);
- [DllImport("Kernel32.Dll")]
- extern static int GetCurrentProcess();
- static void Main()
- {
- //SetProcessPriorityBoost(GetCurrentProcess(), 2);
- const int Count = 300;
- const int Interval = 100;
- int[] busySpan = new int[Count];
- int[] idleSpan = new int[Count];
- int half = Interval / 2;
- for(int i = 0;i < Count;i++)
- {
- busySpan[i] = (int)(Math.Sin(2d * Math.PI * i / Count) * half + half);
- idleSpan[i] = Interval - busySpan[i];
- }
- long t = 0;
- int j = 0;
- while(true)
- {
- t = DateTime.Now.Ticks;
- while((DateTime.Now.Ticks - t) <= busySpan[j] * 10000)
- ;
- Thread.Sleep(idleSpan[j]);
- j = ++j % Count;
- }
- }
- }
在自己的机器上是画出来了。