可以构建在后台线程的形式,然后显示在UI线程 [英] Possible to construct form on background thread, then display on UI thread

查看:101
本文介绍了可以构建在后台线程的形式,然后显示在UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:只是总结一下我的问题归结为:

UPDATE: Just to summarize what my question has boiled down to:

我希望构建.NET窗体和控件并未造成任何窗口句柄 - 希望这一过程被推迟到Form.Show/Form.ShowDialog

I was hoping that constructing .NET forms and controls did NOT create any window handles -- hoping that process was delayed until Form.Show/Form.ShowDialog

任何人都可以证实或否认这是否是真的?

Can anyone confirm or deny whether that is true?

我有一个大的WinForms与标签控件到窗体上很多很多的控制,同时加载了几秒钟的停顿形成。我已经收窄到设计中的InitializeComponent生成的代码,而不是任何我在构造函数或逻辑的OnLoad的。

I've got a large WinForms form with tab control, many many controls on the form, that pauses while loading for a couple seconds. I've narrowed it down to the designer generated code in InitializeComponent, rather than any of my logic in the constructor or OnLoad.

我很清楚,我可以' T为试图用比主UI线程以外的任何线程的UI交互,但我想要做的就是让应用程序预载这种形式(运行构造函数)的背景,所以它的准备显示器对UI线程即刻只要用户想打开它。然而,在后台线程施工时,在这条线的设计:

I'm well aware that I can't be trying to interact with the UI on any thread other than the main UI thread, but what I'd like to do is to have the application pre-load this form (run the constructor) in the background, so it's ready for display on the UI thread instantly as soon as the user wants to open it. However, when constructing in a background thread, on this line in the designer:

this.cmbComboBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;



我得到的错误

I'm getting the error

当前线程必须设置为单
线程单元(STA)模式可制成OLE
调用之前。确保您的
主要功能有请将STAThreadAttribute
标就可以了。

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

现在,这是中途下设计文件,这给了我希望的一般的这个策略会奏效。但这一行似乎是为了要立即揭开序幕某种OLE调用。

Now this is halfway down the designer file, which gives me hope that in general this strategy will work. But this particular line seems to be trying to instantly kick off some kind of OLE call.

任何想法?

编辑:

我想我不会让自己清楚在这里。延迟似乎bazillion控件的设计器生成的代码在施工期间举行。

I think I'm not making myself clear here. The delay seems to take place during the construction of a bazillion controls during the designer-generated code.

我的希望是,这一切的初始化代码发生没有实际试图触摸任何真正的Win32窗口对象,因为形式实际上并没有尚未显示。

My hope was that all this initialization code took place without actually trying to touch any real Win32 window objects since the form hasn't actually been shown yet.

这是我可以设置(例如)标签的文本和位置从这个后台线程的事实了我希望这是真的。然而,它可能不适合所有属性如此。

The fact that I can set (for example) Label texts and positions from this background thread gave me hope that this was true. However it may not be true for all properties.

推荐答案

答案是否定的。

如果你不是GUI线程你永远无法证明它的其他任何线程创建的窗口句柄。

If you create a window handle on any thread other than the GUI thread you can never show it.

编辑:这是完全可以创建窗体和控件和
比主界面线程的线程上显示出来。当然,如果
你这样做,你只能访问从创建它的线程
线程图形用户界面多,但它是可能的。 - 阿什利亨德森

It is completely possible to create Forms and controls and display them in a thread other than the main GUI thread. Of course if you do this you can only access the multi threaded GUI from the thread that created it, but it is possible. – Ashley Henderson

您需要在BG线程执行任何繁重,然后将数据加载到你的GUI控件

You need to perform any heavy lifting on a bg thread and then load the data into you GUI widget

这篇关于可以构建在后台线程的形式,然后显示在UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆