none
关于performance类的问题 RRS feed

  • 问题

  • 我现在想从performance里面读取一点数据
    比如我取了CPU使用率的值:
                PerformanceCounter temp11 = new PerformanceCounter();
                temp11.CategoryName = "Processor";
                temp11.CounterName = "% Processor Time";
                temp11.InstanceName = "_Total";

    我从性能监视器里面,看到这个是有值在不断的变化中,但我从页面中response.write(temp11.NextValue().ToString())这个值怎么一直是0,不知道哪里有问题?
    2010年1月12日 6:20

答案

  • 原理是一样的, 但 asp.net 和 winform 的机制不同,实现方式也不同。

    ASP.NET 方式。

    后台代码
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Threading;
    
    public partial class Default5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            Response.Buffer = true;
            bool isOutput = false;
            Response.Write("");
            Response.Flush();
    
            System.Diagnostics.PerformanceCounter performanceCounter = new System.Diagnostics.PerformanceCounter();//新建一个性能计数器 
            
            performanceCounter.CategoryName = "Processor";
            performanceCounter.CounterName = "% Processor Time";
            performanceCounter.InstanceName = "_Total";
            
            int lastsecond = 0;
            while (Response.IsClientConnected)
            {
                Thread.Sleep(500);
                if (DateTime.Now.Second != lastsecond && !isOutput)
                {
                    lastsecond = DateTime.Now.Second;
                    Response.Write("<script>document.getElementById('cpuHolder').innerText = '" + performanceCounter.NextValue().ToString() + "'; </script> ");
                    Response.Flush();
                    isOutput = true;
                }
                else
                {
                    isOutput = false;
                }
            }
        }
    }
    

    前台HTML
    --------------------------------------------------------
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>PerformanceCounter Test</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="cpuHolder">
            </div>
        </form>
    </body>
    </html>




    知识改变命运,奋斗成就人生!
    2010年1月12日 7:06
    版主

全部回复

  • 你好!

    你需要使用一个计时器,每过一段时间去取一下值。


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace WindowsFormsApplication34
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                System.Diagnostics.PerformanceCounter performanceCounter = new System.Diagnostics.PerformanceCounter();//新建一个性能计数器 
                //设置属性 
                performanceCounter.CategoryName = "Processor";
                performanceCounter.CounterName = "% Processor Time";
                performanceCounter.InstanceName = "_Total";
    
                Timer timer = new Timer();
    
                timer.Enabled = true;
                timer.Interval = 500;
    
                timer.Tick += new EventHandler(delegate(object o, EventArgs e)
                {
                    this.Text = performanceCounter.NextValue().ToString();
                });
    
            }
        }
    }
    


    知识改变命运,奋斗成就人生!
    2010年1月12日 6:40
    版主
  • 我看楼主这个是winform,我这个是WEB的有关系嘛?

    2010年1月12日 6:47
  • 道理都是一样的啊。
    知识改变命运,奋斗成就人生!
    2010年1月12日 6:48
    版主
  • 晕了,这个加了以后,这个值就一直变成100了。
    这个监视CPU状态这么困难啊

    2010年1月12日 6:54
  • 我就要显示一下,当前页面加载后,当前CPU的使用率
    这第一次要么就是0,加了一个TIMER后,这个值就一直是100晕了

    2010年1月12日 6:54
  • 原理是一样的, 但 asp.net 和 winform 的机制不同,实现方式也不同。

    ASP.NET 方式。

    后台代码
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Threading;
    
    public partial class Default5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            Response.Buffer = true;
            bool isOutput = false;
            Response.Write("");
            Response.Flush();
    
            System.Diagnostics.PerformanceCounter performanceCounter = new System.Diagnostics.PerformanceCounter();//新建一个性能计数器 
            
            performanceCounter.CategoryName = "Processor";
            performanceCounter.CounterName = "% Processor Time";
            performanceCounter.InstanceName = "_Total";
            
            int lastsecond = 0;
            while (Response.IsClientConnected)
            {
                Thread.Sleep(500);
                if (DateTime.Now.Second != lastsecond && !isOutput)
                {
                    lastsecond = DateTime.Now.Second;
                    Response.Write("<script>document.getElementById('cpuHolder').innerText = '" + performanceCounter.NextValue().ToString() + "'; </script> ");
                    Response.Flush();
                    isOutput = true;
                }
                else
                {
                    isOutput = false;
                }
            }
        }
    }
    

    前台HTML
    --------------------------------------------------------
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>PerformanceCounter Test</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="cpuHolder">
            </div>
        </form>
    </body>
    </html>




    知识改变命运,奋斗成就人生!
    2010年1月12日 7:06
    版主
  • 但二位版主还有一个问题
    这样做是可以读到CPU的使用率,但页面在不停的刷,有没有办法把在前台看不到这个页面在刷新,我在外面放了一个AJAX的无刷新不行,不起效果。
    2010年1月26日 5:57
  • 使用隐藏 iframe 来处理。
    知识改变命运,奋斗成就人生!
    2010年1月26日 6:19
    版主
  • 不明白,这个放在IFRAME里面,如果把这个IFRAME给隐藏了,那这个还怎么显示呢?
    我说的是窗口下面有一个进度条,每更新一次CPU,那个进度条就一直在不停的刷新

    2010年1月26日 6:25
  • 就像X。X。Y你给的代码,这样跑起来不是页面一直在刷新嘛
    2010年1月26日 6:27
  • 将这个获取 CPU 参数的页面使用 iframe 隐藏起来(iframe 加到进度条的页面)。获取 CPU 参数页面,每过一段时间输出一段脚本去更新你的进度条。
    知识改变命运,奋斗成就人生!
    2010年1月26日 6:37
    版主