控制当前线程的CPU使用量

经过两个多小时的google和试探,终于搞定了一段代码.通过这个代码可以控制当前线程的CPU使用量.下面是代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
namespace WindowsFormsApplication1
{
  public class CPU
  {
    private int mUsage = 20;
    public int MaximumUsage
    {
      get
      {
        return this.mUsage;
      }
      set
      {
        if (value > 0 && value < 100)
        {
          this.mUsage = value;
        }
      }
    }
    private long LastTotalThreadTime { get; set; }
    private long LastTotalSystemTime { get; set; }
    public CPU(int usage)
    {
      this.MaximumUsage = usage;
      this.LastTotalThreadTime = 0;
      this.LastTotalSystemTime = 0;
      this.LastTotalSystemTime = this.GetSystemTime();
      this.LastTotalThreadTime = this.GetThreadTime();
    }
    public void Sleep()
    {
      long systemTime, threadTime;
      systemTime = this.GetSystemTime();
      threadTime = this.GetThreadTime();
      if (threadTime > 0)
      {
        long sleepTimes = ((threadTime - this.LastTotalThreadTime) * this.MaximumUsage)
          - (systemTime - this.LastTotalSystemTime);
        sleepTimes /= 10000;
        if (sleepTimes > 0)
        {
          TimeSpan ts = TimeSpan.FromMilliseconds(sleepTimes);
          Thread.Sleep(ts);
          this.LastTotalThreadTime = threadTime;
          this.LastTotalSystemTime = systemTime;
        }
      }

    }
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool GetSystemTimes(
                out FILETIME lpIdleTime,
                out FILETIME lpKernelTime,
                out FILETIME lpUserTime
                );
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool GetThreadTimes(IntPtr hThread,
      out FILETIME lpCreationTime,
      out FILETIME lpExitTime,
      out FILETIME lpKernelTime,
      out FILETIME lpUserTime);

    [DllImport("kernel32.dll")]
    private static extern IntPtr GetCurrentThread();
    private long GetLong(FILETIME ft)
    {
      long result = ((long)ft.dwHighDateTime << 32) + ft.dwLowDateTime;
      return result;
    }
    private long GetSystemTime()
    {
      long result = this.LastTotalSystemTime;
      FILETIME ftIdleTime, ftKernelTime, ftUserTime;
      if (GetSystemTimes(out ftIdleTime, out ftKernelTime, out ftUserTime))
      {
        long idleTime = this.GetLong(ftIdleTime);
        long kernelTime = this.GetLong(ftKernelTime);
        long userTime = this.GetLong(ftUserTime);
        result = idleTime + kernelTime + userTime;
      }
      return result;
    }
    private long GetThreadTime()
    {
      long result = this.LastTotalThreadTime;
      FILETIME thCreationTime, thExitTime, thKernelTime, thUserTime;
      IntPtr threadId = GetCurrentThread();
      if (GetThreadTimes(threadId, out thCreationTime, out thExitTime,
        out thKernelTime, out thUserTime))
      {
        long kernelTime = this.GetLong(thKernelTime);
        long userTime = this.GetLong(thUserTime);
        result = kernelTime + userTime;
      }
      return result;
    }
    
  }
}

没有太多的技术含量,就是调用了3个Windows的API然后计算一下当前线程占用的时间和整个机器运行的时间之间的比值,比值超过某个特定的域就让当前线程休息一会.休眠的时间也是根据那个CPU占用率计算出来的,这样可以将当前线程的CPU占有率大约控制在一个指定的值周围。


下面是如何使用的代码:

CPU cpu = new CPU(20);
      while (true)
      {
        long result = 0;
        int times = new Random().Next(10000);
        for (int i = 0; i < times; i++)
        {
          result += i;
        }        
        cpu.Sleep();
      }


————————————————

版权声明:本文为CSDN博主「zhuliangxiong」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/zhuliangxiong/article/details/4097889


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864