【发布时间】:2019-09-18 22:01:03
【问题描述】:
我想让一个按钮运行一些代码,然后在 Windows 窗体中再次按下它自己。
当我调用按钮本身时出现错误:
System.StackOverflowException
HResult=0x800703E9
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
我想制作一个资源监控程序来熟悉C#。现在我被困在字符串处理和显示不断变化的字符串上。 我使用随机生成的数字作为占位符,我想永久显示和更改它,模仿真实的数据拉取。
我的代码:
public void Start_Click(object sender, EventArgs e){
var usage =new CpuUsage(); //placeholder class for getting the CPU use data
usage.setCPU(); //gets a random number
this.CPU.Text = usage.cpuUsage; //show the usage data on a textbox
Start_Click(null, EventArgs.Empty); //call this button again here
}
我想要得到的东西看起来像:
- 获取数据
- 使用文本框显示数据
- 重做这个
【问题讨论】:
-
这不是一个好主意。如果您想获取重复的随机数据来更新表单上的字段,请查看this answer 了解更多信息
-
轮询是术语,而不是拉动。并且按钮不是正确的工具。使用计时器。理想情况下是 Windows 窗体计时器。一键点击启动计时器。 Anotehr 阻止了它。每一个滴答声,你都在做你的工作。只要你把invervall至少保持在两位数,而且这个过程快了一半,就没什么好担心的了。
标签: c# recursion button windows-forms-designer