智能表单更新想法

发布于 2024-08-18 18:50:23 字数 282 浏览 11 评论 0原文

用 C# 开始我的第一个严肃的 GUI 应用程序。

我有一个类正在处理和执行很多事情,我可以将 GUI 控制器类中的方法插入其中,并在适当的时候获取事件。

我有几个列表框和其他控件,我想在更新此信息时更新。但是,我想稍微限制一下。

对于我的 GUI 表单上的控件,如果我允许它(每次委托触发时),它们会经常更新,我应该放置一个每秒更新一次的计时器。对于其他类,只要控制数据流的类就让它们更新即可。

这是专业人士会做的事吗?如果可以的话,我讨厌使用计时器,但我担心表单会根据情况疯狂更新。

Starting my first serious GUI app in C#.

I have a class that is handling and doing lots of things that I can plug methods from my GUI controller class into and get events when it appropriate.

I have several listboxes and other controls I want to update whenever this information is updated. However, I would like to throttle this a bit.

For the controls on my GUI form that would update very often if I let it (each time a delegate fires) should I put a timer that updates perhaps once a second. And with the others simply have them update whenever the class that controls the data flow.

Is this what a professional would do? I hate to use a timer if I can avoid but I am afraid of the form updating like crazy, depending on the circumstances.

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

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

发布评论

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

评论(1

心清如水 2024-08-25 18:50:23

我不认为使用计时器有什么问题。从本质上讲,这就是限制 - 每秒不允许超过 x 个事件。重要的是让你的计时器检查以确保在盲目更新 UI 之前某些内容已发生变化 - 你不希望它无缘无故地每半秒进行一次屏幕更新。

因此 - 像平常一样挂钩事件,但不要立即更新,而是设置一个标志。计时器可以在更新 UI 之前检查该标志。

或者,如果您坚持优化,可以让事件启动计时器,并让计时器在更新后立即自行停止;这样,除非最近发生了事件,否则您的计时器根本不会运行。

对于这是否是“专业人士”会做的问题,我无法给出明确的答案,因为该领域的专业人士似乎有截然不同的做很多事情的方式。

I don't see anything wrong with using a timer. That's what throttling is, essentially - don't allow more than x events per second. The important thing is to have your timer check to make sure that something has changed before blindly updating the UI - you don't want it doing screen updates every half-second for no reason.

So - hook the events like you normally would, but instead of doing an immediate update, set a flag. The timer can check the flag before updating the UI.

Or, if you're a stickler for optimization, have the events start the timer, and have the timer stop itself immediately after an update; that way, your timer isn't running at all unless there has been a recent event.

I can't give a definitive answer to the question of whether or not this is what "a professional" would do, as professionals in this field seem to have wildly different ways of doing many things.

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