Write programs + control cpu usage-4 + draw a sine curve of CPU usage 2 + C # + Implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.