我有一个窗体(complexForm在代码中)与多个控件,需要一些时间来加载。所以我决定放入一个单独的线程,以减少初始加载时间。除了等待表单上的标签控件(代码中的Form1)一开始不会显示,一切正常;在Form1出发之前只需要一秒钟的时间。所以我的问题是,为什么不显示标签控制?控件不显示,如果运行在后台线程(c#winform)
[STAThread]
static void Main()
{
Thread thread = new Thread(delegate()
{
var wait = new Form1(); //simple form with a label control with text "please wait"
wait.Show();
var complexUI = new complexForm();// this takes long time to load
wait.Dispose();// it will go off even without this method
// MessageBox.Show("loaded");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Priority = ThreadPriority.Highest;
thread.IsBackground = true;
thread.Start();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new main());
}
如果您使用多个'thread',为什么要将'ApartmentState'设置为单线程('[STAThread]'和'ApartmentState.STA')? – Brian 2013-02-15 16:35:13
你想实现什么? 'var complexUI = new complexForm(); //这需要很长时间才能加载'在complexForm构造函数中执行什么操作会导致长时间加载? – 2013-02-15 16:37:00
实际上,我有DevExpress richeditcontrols在其中需要很长时间才能加载的ribbonbar。请参阅此URL http://www.devexpress.com/Support/Center/p/Q406398.aspx – user1746821 2013-02-15 16:42:43