积极答复者
关于performance类的问题

问题
-
我现在想从performance里面读取一点数据
比如我取了CPU使用率的值:
PerformanceCounter temp11 = new PerformanceCounter();
temp11.CategoryName = "Processor";
temp11.CounterName = "% Processor Time";
temp11.InstanceName = "_Total";
我从性能监视器里面,看到这个是有值在不断的变化中,但我从页面中response.write(temp11.NextValue().ToString())这个值怎么一直是0,不知道哪里有问题?
答案
-
原理是一样的, 但 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>
知识改变命运,奋斗成就人生!- 已建议为答案 KeFang Chen 2010年1月13日 2:35
- 已标记为答案 mldarkModerator 2010年1月13日 14:11
全部回复
-
你好!你需要使用一个计时器,每过一段时间去取一下值。
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(); }); } } }
知识改变命运,奋斗成就人生! -
原理是一样的, 但 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>
知识改变命运,奋斗成就人生!- 已建议为答案 KeFang Chen 2010年1月13日 2:35
- 已标记为答案 mldarkModerator 2010年1月13日 14:11