[英]Is there a WinForms control to show and hide another WinForms control?
[英]winforms - Move control to another thread
是否可以移動控件或至少將控件復制到創建該控件的另一個線程上。 原因是我希望控件完全在后台線程中加載,然后在完成加載后將控件移至另一個線程。 例如:
BackgrundworkRunasync(object sender, DoWorkEventArgs e )
{
var GetData = GetData();
CreateControl mycontrol = new CreateControl() //Tyep of WindowsForm
mycontrol.Data = GetData;
e.Result = mycontrol;
}
BackGroundWorkerComplete ( object sender, RunWorkerCompletedEventArgs e )
{
CreateControl con = (CreateControl)e.Result;
con.mdiparent = this;
con.Show();
//Of course this is a cross threading exception. Can I move this control to the current thread or even create a control in the current thread and do a deep copy? Optimally I just want to move the control to another thread, can you do this?
}
不,不可能。 該控件必須在主線程上創建。
您應該像這樣修改代碼:
BackgrundworkRunasync(object sender, DoWorkEventArgs e )
{
e.Result = GetData();
}
BackGroundWorkerComplete ( object sender, RunWorkerCompletedEventArgs e )
{
CreateControl mycontrol = new CreateControl() //Tyep of WindowsForm
mycontrol.Data = e.Result;
myControl.mdiparent = this;
myControl.Show();
}
不,這是不允許的。 所有控件必須由單線程提供服務。 它是您用來創建窗口的線程,通常是進程的第一個線程。
您可以使用Invoke從單獨的線程更新控件。 在這里看看
Invoke((MethodInvoker)delegate
{
//use control
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.