嘿伙计们我是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"); }更多推荐
发布评论