11
我有一個Silverlight 5瀏覽器應用程序。在非UI線程中創建UserControl Silverlight 5瀏覽器應用程序
有一類
public class ActivityControl:UserControl {
public void LoadSubControls() {
//Creates Other UserControls, does calculations and is very slow..No refactoring..
}
}
我需要創建這個類的多個實例,並調用運行時的方法LoadSubControls。
public class BasicContainer:UserControl {
public void CreateMultipleActivityControls() {
for (int i = 0; i < 1000; i++) {
ActivityControl c = new ActivityControl(); ====> I need to call this in a different thread but causes Invalid Cross Thread Exception
c.LoadSubControls();
}
}
}
有沒有什麼辦法來創建多個UI線程,以避免無效的跨線程異常?
爲了性能方面的原因,我需要多線程,因爲方法調用非常慢,UI凍結。
有沒有辦法在Silverlight中調用方法SetSyncronizationContext
(這是[SecurityCritical])?
所有的控件都直接在gui中看到嗎?你可以創造他們懶惰? –
在「LoadSubControls」方法中,我測量排列子控件的UpdateLayout以生成圖像。你的意思是我不應該運行InitializeComponent()? – George
我也想把你的方法拆分成更小的塊,並且每次你真的需要在UI中放置一個控件時,只回調到UI線程。 –