首先,我需要說我很喜歡WPF和C#。 應用程序:創建曼德爾布羅圖片(GUI) 我分派工作完全此這種情況下:WPF Dispatcher {「調用線程不能訪問這個對象,因爲一個不同的線程擁有它。」}
private void progressBarRefresh(){
while ((con.Progress) < 99)
{
progressBar1.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
{
progressBar1.Value = con.Progress;
}
));
}
}
我得到的消息(標題)特林當與下面的代碼來做到這一點:
bmp = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, stride);
this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
{
img.Source = bmp;
ViewBox.Child = img; //vllt am schluss
}
));
我將嘗試解釋我的程序如何工作。 我創建了一個新的線程(因爲GUI不響應)來計算像素和顏色。在這個線程(方法)中,在計算準備就緒後,我使用Dispatcher刷新ViewBox中的圖像。
當我不把計算放在單獨的線程中時,我可以刷新或構建我的圖像。
您不需要在UI線程上創建圖像。你不能從一個線程在另一個線程上使用它(而.NET假定除非你將它標記爲* freeze *,否則它將被再次修改)。 – 2010-06-06 02:18:33