定期更新屏幕数据的模式

发布于 2024-09-16 07:16:58 字数 562 浏览 4 评论 0原文

我是 GUI 编程领域的新手,我正在使用 IronPython 和 WinForms 编写一个小型 GUI 应用程序。它从外部设备读取值并将它们显示在 ListView 组件中(名称、值)。我想以一定的固定速率定期执行ListView组件的读取和更新。

我有以下想法来实现这一点:

  • 一个计时器,它定期直接在 OnTick 处理程序中触发读取/屏幕更新
  • 一个计时器,其 OnTick 处理程序触发一个 BackgroundWorker 来执行读取/更新

因为第一个解决方案将阻止 GUI,直到读取/更新循环已完成,这取决于从设备读取的值的数量,可能需要一些时间,我认为BackgroundWorker可能是一个更好的解决方案。我可能想添加一些功能来稍后操作 ListView 项目(添加、删除值等),而阻塞的 GUI 似乎不是一个好主意。

是否有某种设计模式或更好的方法来完成读取/更新屏幕数据?

注意:任何代码示例都可以是 IronPython 或 C#。从 C# 到 IronPython 的转换非常简单。谢谢!

I am new to the world of GUI programming and I am writing a little GUI app using IronPython and WinForms. It reads values from an external device and displays them in a ListView component (name, value). I want to periodically perform the reading and updating of the ListView component at a certain fixed rate.

I had the following ideas to accomplish this:

  • A timer, which periodically triggers the read/screen update directly in the OnTick handler
  • A timer, whose OnTick handler triggers a BackgroundWorker to perform the read/update

Since the first solution will block the GUI until the read/update loop is done, which, depending on the number of values being read from the device, could take some time, I think the BackgroundWorker might be a better solution. I might want to add some functionality to manipulate the ListView items later (add, remove values etc.) and a blocked GUI does not seem like a good idea.

Is there a certain design pattern or a better way to accomplish reading/updating screen data?

NOTE: Any code examples can be IronPython or C#. The conversion from C# to IronPython is pretty straight forward. Thanks!

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

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

发布评论

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

评论(3

假面具 2024-09-23 07:16:58

就我个人而言,我有一个线程负责从设备中读取值并将数据存储在数据结构中,然后是一个 System.Windows.Forms.Timer (有 3 个 Timer 在 .NET 中,这是线程中唯一可以安全更新控件的线程,从该数据结构中读取值并更新 UI。您可能需要同步该数据结构,也可能不需要。

这样,设备可以花尽可能长的时间返回数据,也可以每秒向您推送数百万行数据,并且 UI 线程仍将以您预定的滴答率(可能每 100 毫秒?)进行轮询。由于计时器的滴答声只是从内存中读取数据,因此 UI 不会阻塞 IO。

Personally, I'd have one thread that's responsible for reading values out of the device and storing the data in a data structure, and then a System.Windows.Forms.Timer (there's 3 Timers in .NET, this is the only one that ticks in the thread that's safe to update your controls) to read values out of that data structure and update the UI. You may need to synchronise that data structure, you may not.

That way the device can take as long as it likes to return data, or it can push as many millions of rows per second at you, and the UI thread will still poll at your predetermined tick rate (probably every 100 msec?). Since your timer ticks are just reading data out of memory, the UI won't block for IO.

青春如此纠结 2024-09-23 07:16:58

当您在后台有大量工作要做时,BackgroundWorker 是首选。

使用Timer 触发一个函数,该函数将在第二个线程中执行必要的工作。

它不会阻塞 UI。 (不要忘记更新 UI 线程上的控件)

System.Threading.Thread newThread;
newThread = new System.Threading.Thread(anObject.AMethod);

http://msdn.microsoft.com/fr-fr /library/ms173178(VS.80).aspx

The BackgroundWorker is prefered when you have lot of work to do in the background.

Use a Timer to trigger a function that will do the necessary work in a second thread.

It won't block the UI. (don't forget to update the controls on the UI thread).

System.Threading.Thread newThread;
newThread = new System.Threading.Thread(anObject.AMethod);

http://msdn.microsoft.com/fr-fr/library/ms173178(VS.80).aspx

梦在夏天 2024-09-23 07:16:58

除了让计时器和后台工作线程工作之外,另一种选择是使用 System.Threading.Timer,这将定期在线程上执行您的方法,一旦您获得数据,就可以使用Control.Invoke 或 Control.BeginInvoke 在 UI 线程中更新控件。

Another option rather than getting the Timer and the Background worker thread working would be to use the System.Threading.Timer, this will execute your method on a thread on a regular interval and once you have the data you can use Control.Invoke or Control.BeginInvoke to update the control in the UI thread.

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