无法使用与其父 Freezable 属于不同线程的 DependencyObject
我有一个 wpf 表单,我想在用户从控件中做出选择后立即显示加载弹出窗口,因为由于数据库不是本地的,数据的加载可能需要很长时间。我让一切正常工作,直到我为弹出窗口创建线程。
这是我创建线程的地方:
public void Start()
{
if (_parent != null)
_parent.IsEnabled = false;
_thread = new Thread(RunThread);
_thread.IsBackground = true;
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
_threadStarted = true;
SetProgressMaxValue(10);
Thread th = new Thread(UpdateProgressBar);
th.IsBackground = true;
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
和线程方法:
private void RunThread()
{
_window = new WindowBusyPopup(IsCancellable);
_window.Closed += new EventHandler(WaitingWindowClosed);
_window.ShowDialog();
}
现在执行的那一刻我收到此错误:
无法使用与其父 Freezable 属于不同线程的 DependencyObject。
任何帮助将不胜感激:)
Ive got a wpf form, from which i want to display a loading popup as soon as the user makes a choice from the controls, because the loading of the data could take long seeing as the Database is not Local. I got everything working up until where i create the thread for the popup window.
This is where i create my Thread:
public void Start()
{
if (_parent != null)
_parent.IsEnabled = false;
_thread = new Thread(RunThread);
_thread.IsBackground = true;
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
_threadStarted = true;
SetProgressMaxValue(10);
Thread th = new Thread(UpdateProgressBar);
th.IsBackground = true;
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
And the thread Method:
private void RunThread()
{
_window = new WindowBusyPopup(IsCancellable);
_window.Closed += new EventHandler(WaitingWindowClosed);
_window.ShowDialog();
}
Now the moment that executes i Get this error :
Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
Any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用表单的 Dispatcher 属性。
Dispatcher.BeginInvoke(...)
或者只使用 BackgroundWorker 类,因为它有一个名为 ReportProgress() 的方法来报告进度百分比。当您可以刷新进度条的值或其他内容时,这将触发 ProgressChanged 事件...
Try to use the Dispatcher property of the form.
Dispatcher.BeginInvoke(...)
Or just use the BackgroundWorker class, because it has a method called ReportProgress() to report the progress percentage. This will fire the ProgressChanged event, when you can refresh the value of the progressbar or something...
无法使用与其父 Freezable 属于不同线程的 DependencyObject。
出现此错误是因为您尝试使用在 STA 中的不同线程中创建的资源(UIElement 类型)线程(您用来显示弹出窗口)。
在您的情况下,它看起来像第二个线程 Thread th = new Thread(UpdateProgressBar); ,正在尝试操作 WindowBusyPopup 中的 UI。由于弹出窗口由不同的线程拥有,因此您会收到此异常。
可能的解决方案:(据我所知,您没有显示函数 UpdateProgressBar 的实现)
Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
This error is observed because your are trying to use a resource(of the type UIElement) which was created in a different thread in your STA thread(which you are using to show the popup window).
In your case it looks like the second thread Thread th = new Thread(UpdateProgressBar); , is trying to manipulate the UI in the WindowBusyPopup. As the popup is owned by a different thread you are getting this exception.
Possible Solution: (as I see you dont show the implementation of the function UpdateProgressBar)