如何在texbox.text窗体中显示性能计数器结果c#(How to show performance counter results in texbox.text windows forms c#

编程入门 行业动态 更新时间:2024-10-26 14:38:40
如何在texbox.text窗体中显示性能计数器结果c#(How to show performance counter results in texbox.text windows forms c#)

嘿伙计们我是c#的新手,我从阅读这个社区的答案中学到了很多东西。

我试图通过控制台创建一个性能监控系统,但是当涉及到Windows窗体时,我在向用户查看结果时遇到了问题。 这里是我使用的代码(是的,我包括System.Diagnostics)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //test PerformanceCounter CpuCount = new PerformanceCounter("Processor Information", "%Processor Time", "_Total"); //textBox1.Text = CpuCount.NextValue().ToString(); textBox1.Text = String.Format("{0} %", CpuCount.NextValue()); } } } //textBox1.Text = CpuCount.NextValue().ToString(); //textBox1.Text = String.Format("{0} %", CpuCount.NextValue());

这两行是我在搜索时得到的输出显示在textBox1.Text 但是当我运行程序时它显示为空白。

请问有人给我一个提示吗?

Hey guys I`m new to c# and I learned a lot from reading answers on this community.

I`m trying to create a performance monitoring system I did through the console ,but when it comes to windows forms I have a problem in viewing my results to the user. here is the code I used (yes i included System.Diagnostics)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //test PerformanceCounter CpuCount = new PerformanceCounter("Processor Information", "%Processor Time", "_Total"); //textBox1.Text = CpuCount.NextValue().ToString(); textBox1.Text = String.Format("{0} %", CpuCount.NextValue()); } } } //textBox1.Text = CpuCount.NextValue().ToString(); //textBox1.Text = String.Format("{0} %", CpuCount.NextValue());

these two lines I found while searching to get the output to show in textBox1.Text but it shows as blank when I run the program.

Can anyone give me a hint please?

最满意答案

尝试在%和Processor Time之间放置一个空格

PerformanceCounter CpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

要更新计数器,您需要一个计时器。

private PerformanceCounter cpuCounter; public Form1() { InitializeComponent(); InitialiseCPUCounter(); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { float tmp = cpuCounter.NextValue(); textBox1.Text = String.Format("{0} %", tmp); } private void InitialiseCPUCounter() { cpuCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); }

Try putting a space between % and Processor Time

PerformanceCounter CpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

To update the counter, you'll need a timer.

private PerformanceCounter cpuCounter; public Form1() { InitializeComponent(); InitialiseCPUCounter(); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { float tmp = cpuCounter.NextValue(); textBox1.Text = String.Format("{0} %", tmp); } private void InitialiseCPUCounter() { cpuCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); }

更多推荐

本文发布于:2023-08-07 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   计数器   性能   如何在   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!