找出移动表单应用程序的用户界面中的控件未更新的原因

发布于 2024-11-29 19:24:42 字数 758 浏览 0 评论 0原文

背景

这是一个 Compact Framework 3.5 Forms 应用程序。在显示表单的过程中,静态(这意味着按原样放置在表单中的控件)控件将填充在用户界面线程中,而可能需要加载数据的控件将填充在单独的线程中从用户界面一。

我使用 Control.Invoke 来将填充控件与用户界面线程中的数据同步。

请注意,用户界面不会卡住,并且会按预期运行。

问题

ListBox这样的控件,即使它们有项目,这些项目也不会显示在用户界面中。

每当添加一个项目时,代码都会调用 ListBox.Update 方法来重绘它。


你能给我一些关于正在发生的事情的提示吗?

注意:我可以在此处粘贴一些代码,但我发现这没什么用,因为它只是通过创建一个线程来启动一个线程 (new Thread(...).Start()),并且在该线程执行期间,我调用某种形式的 Invoke 方法。

预先感谢您的建议和时间。


更新 如果我使用 Control.Invoke 在与 UI 线程操作同步的情况下创建一个像 ListBox 这样的控件,我就可以正常工作了。无论如何,当使用设计器创建控件时,我仍然需要解决这个问题,因为以这种方式放置控件更容易。谢谢。

Background

This is a Compact Framework 3.5 Forms application. In the process of showing a form, static (this means controls placed in the form as is) controls are populated in the user interface thread, while ones which may need loading data, are populated in a separate thread from the user interface one.

I'm using Control.Invoke in order to synchronize populating controls with data in the user interface thread.

Note user interface doesn't get stuck, and it's running as expected.

Problem

Controls like ListBox, even if they've items, these aren't shown in the user interface.

Whenever an item is added, code calls ListBox.Update method in order to redraw it.


Will you give me some hint about what's happening?

NOTE: I could paste here some code, but I find that useless, because it's just about starting a thread by creating a one (new Thread(...).Start()) and, during this thread's execution, I call some form Invoke method.

Thank you in advance for your suggestions and time.


UPDATE
I got it working if I create a control like ListBox in the synced with UI thread action using Control.Invoke. Anyway, I've still need to solve that when control is created using the designer, because it's easier to place controls in that way. Thanks.

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

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

发布评论

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

评论(1

蓝眼睛不忧郁 2024-12-06 19:24:42

我注意到在单独的线程中创建的许多控件在返回到执行的主线程时表现得很奇怪或完全消失。也许他们成为孤儿或者……什么的。

以下是对我最有效的方法:将控件放在表单上(如果您不想看到它们,请设置 Visible=false、Width=0 等),然后将数据从线程传回。当数据返回时,用数据填充您的控件。通常,我将创建一个包含我正在查找的返回值的类,并返回该类的实例。

也就是说,我必须脱帽致敬,克里斯·塔克确实知道他在做什么。自从 XP 推出以来,我就一直接受他的指导,并且我使用 Outlook Express 来阅读 Microsoft 的新闻组。

I've noticed lots of controls that get created in a separate thread behave peculiarly or vanish altogether when they are returned to the main thread of execution. Perhaps they become orphan or ...something.

Here's what has worked best for me: Place the controls on the form (set Visible=false, Width=0, etc., if you don't want to see them) and just pass the data back from your thread. When the data comes back, populate your controls with the data. Typically, I will create a class that contains the returned values that I am looking for, and return an instance of the class.

That said, I must tip my hat and say Chris Tacke does know what he's doing. I've taken guidance from him since XP came out and I was using Outlook Express to read from Microsoft's Newsgroups.

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