C#:在单独的线程上预加载表单
我有一些表单需要一些时间才能打开,因为它们当前从 Load 事件处理程序中的数据库获取一些内容。
是否可以以某种方式在单独的线程中加载表单并在完成后向用户显示它们?
如果加载它们以便触发 Load 事件处理程序是不可能的,也许拥有一个 IPreloadable 接口可以使用 Preload 方法来解决这个问题,然后移动缓慢加载的内容进入那个。如果可以从单独的线程显示表单,那么...猜猜我需要使用 Invoke 或类似的东西?
I have some forms that takes a bit of time to open because they currently get some stuff from a database in their Load event handler.
Is it possible to somehow load the forms in a separate thread and show them to the user when that is done?
If loading them so that the Load event handler is fired is not possible, maybe having a IPreloadable
interface might do the trick with a Preload
method, and then move the slow loading contents into that. If it is possible to show the form from a separate thread that is... guess I would need to use Invoke or something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在不同的线程上加载不同的表单,则在表单之间进行调用时必须非常小心 - 您需要使用
Control.Invoke
/< code>BeginInvoke 遍布各处。请注意,虽然每个顶级窗口可以在不同的线程上运行,但窗口中的所有控件都必须在该线程上创建(或者更确切地说,必须创建它们的句柄)。该窗口的线程。
为什么不在后台加载数据库信息,然后当完成后,您可以构建实际的表单并显示它? (在那之前,您可能想要更改为等待光标,或者可能在某处放置“正在加载数据...”状态消息。
If you load different forms on different threads, you'll have to be very careful when you make calls between the forms - you'll need to use
Control.Invoke
/BeginInvoke
all over the place.Note that while each top level window can run on a different thread, all the controls within a window must be created (or rather, they must have their handle created) on the thread for that window.
Why not load the database information in the background, and then when that's finished then you can build the actual form and display it? (Until then you might either want to change to a wait cursor, or possibly put a "Loading data..." status message somewhere.