gpt4 book ai didi

c# - 如何运行一个 while 循环?

转载 作者:行者123 更新时间:2023-11-30 19:38:45 28 4
gpt4 key购买 nike

我想制作一个表单应用程序,每 0.1 秒更新一次 textBox 中的文本所以我这样做了:

private void Start_Stop_Click(object sender, EventArgs e) 
{
double x;
while (true)
{
x = x + 0.00522222222222222222222222222222;
y.Text = x.ToString();
Thread.Sleep(100);
}
}

但是当我运行程序时,它就死机了
(我在控制台应用程序中编写了几乎完全相同的程序,并且运行顺利)。

最佳答案

使用Timer像这样:

double x = 0;
Timer timer1 = new Timer();

public Form1()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Tick += new EventHandler(timer1_Tick);
}

private void Start_Stop_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
}
else
{
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
x = x + 0.00522222222222222222222222222222;
textBox1.Text = x.ToString();
}

关于c# - 如何运行一个 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500796/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com