我有一個窗體(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