首先我需要说我对 WPF 和 C# 很菜鸟。
应用程序:创建 Mandelbrot 图像 (GUI)
我的调度程序在这种情况下工作得很好:
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 没有响应)来计算像素和颜色。在此线程(方法)中,我在计算准备就绪后使用调度程序刷新 ViewBox 中的图像。
当我不将计算放在单独的线程中时,我可以刷新或构建我的图像。
如果您希望在不同线程之间共享该对象,则始终在 UI 线程上创建该对象。稍后当您想要访问该对象时,您可以检查您是否有权访问该对象。如果您无权访问,请使用 UI 线程访问权限重新调用该函数。示例代码如下:
private void YourMethod()
{
if (Application.Current.Dispatcher.CheckAccess())
{
// do whatever you want to do with shared object.
}
else
{
//Other wise re-invoke the method with UI thread access
Application.Current.Dispatcher.Invoke(new System.Action(() => YourMethod()));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)