使用无限循环创建自动更新程序

发布于 2024-09-09 02:46:07 字数 655 浏览 5 评论 0原文

已解决: 你们是最棒的! 我把 goyouidiot_Click 的内容全部取出来,做成一个名为 displayResult 的方法,然后:

 private void t1_TextChanged(object sender, EventArgs e)
       {
        displayResult();
       }

我以前怎么没想到呢?哈哈,谢谢,

这是原始消息:

不久前我开发了一个小软件 计算平均值为 15 数字。代码开始运行时 单击按钮,但我想 将此代码放入无限循环中 开始运行程序,所以 答案将自动更新。这是 我的代码:

private void goyouidiot_Click(对象发件人,

事件参数 e) { 。 。 。 。 }

还有那些不明白的人:

<块引用>

我有 15 个文本框,我想要 当文本框'时运行的方法 内容更改。

Solved:
You guys are the best!
I took al the content of goyouidiot_Click and made it into a method called displayResult, and then:

 private void t1_TextChanged(object sender, EventArgs e)
       {
        displayResult();
       }

How didn't i thought of that before? lol, thx

here is original messege:

No lone ago i built a little software
that calculates an averege of 15
numbers. The code starts to run when
the buttun is clicked, but i want to
put this code in an infinate loop that
starts to run with program, so the
answer will be auto updated. this is
my code:

private void goyouidiot_Click(object sender,

EventArgs e)
{
.
.
.
.
}

and those who havn't understood:

I have 15 text-boxs, and I want
the method to run when the text boxs'
content changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与酒说心事 2024-09-16 02:46:07

您应该只响应更改事件,而不是进行无限循环(这将导致应用程序挂起,除非它位于后台线程 - 一个更大的蠕虫罐)。

如果您的数字在文本框中更新,只需将每个数字的 TextChanged 事件绑定到您的 goyouidiot_Click 方法 - 然后您应该重命名该方法。

编辑

正如 Eric 在他的回答中指出的那样,这些事件起作用的原因是因为实际上后台有一个无限循环来监听更改 - Windows 消息泵。当您在应用程序上调用 Run 方法时,此循环就会启动。

Rather than make an infinite loop (which will cause the application to hang unless it's on a background thread - a much bigger can of worms) you should just respond to change events.

If your numbers are being updated in text boxes, just bind the TextChanged events of each of them to your goyouidiot_Click method - which you should then rename.

Edit

As Eric points out in his answer, the reason these events work is because there is in fact an infinite loop in the background to listen for changes - the Windows message pump. This loop is started when you call the Run method on your application.

您认为必须涉及无限循环的直觉是正确的。但您不想自己编写该循环;运行时库已经为你编写好了。您想要阅读的是事件驱动编程。找到一个好的介绍,例如:

http://www.c -sharpcorner.com/UploadFile/sksaha/EventsinNet11152005043514AM/EventsinNet.aspx

事件驱动编程在幕后工作的方式是存在一个无限循环的代码,用于监视来自操作系统。这些消息代表诸如鼠标点击和打字之类的事情。然后,无限循环代码将这些消息转换为事件触发。您可以监听这些事件触发并在特定事件发生时运行代码。

Your intuition that an infinite loop must be involved is correct. But you don't want to write that loop yourself; the runtime library has already written it for you. What you want to read up on is event-driven programming. Find a good introduction, like, say:

http://www.c-sharpcorner.com/UploadFile/sksaha/EventsinNet11152005043514AM/EventsinNet.aspx

The way event-driven programming works behind the scenes is that there is an infinite loop of code that monitors the state of a queue of messages coming in from the operating system. The messages are representing things like mouse clicks and typing. The infinite loop code then turns those messages into event firings. You can listen to those event firings and run code when particular events happen.

过气美图社 2024-09-16 02:46:07

将其置于无限循环中可能会导致系统 CPU 资源不足,这意味着您需要引入暂停 (Thread.Sleep)。

如果您使用暂停,您也可以使用 Timer 对象 - 有基于表单的 Timer 或基于线程的 Timer (Timer) >System.Windows.Forms 或 System.Threading / System.Timers);

我个人建议使用计时器来按所需的时间间隔进行计时。

线程计时器使用委托/线程池,而基于表单的计时器将消息放置在消息泵上 - 由于创建刻度的方式的开销,两者都不能保证其时间间隔准确。

Putting it in a infinite loop could starve the system of CPU power, meaning you will need to introduce a pause (Thread.Sleep).

If you use a pause, you may as well use a Timer object - there is a form's based Timer or a thread based Timer (System.Windows.Forms, or System.Threading / System.Timers);

I would personally suggest using a timer to tick at a desired interval.

A thread timer uses delegates / thread pool whereas the forms based timer places messages on the message pump - both are not guaranteed to be accurate to their intervals due to the overhead in the way in which a tick is created.

少女的英雄梦 2024-09-16 02:46:07

如果我理解正确(您想在 Winform 应用程序中每 15 分钟更新一次内容),那么最好使用 Timer 类,它将定期运行您的代码。

编辑:如果您想在其中一个文本框中的文本更改时执行一些计算,您应该在 TextChanged 事件(您希望为每个文本框分配相同的处理程序)

If I understand correctly (you want to update something every 15 minutes in your Winform application), then it is better to use the Timer class, which will run your code periodically.

EDIT: If you want to perform some calculations when a text is changed in one of the text boxes, you should do it in an event handler for the TextChanged event of these text boxes (you want to assign the same handler to every text box)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文