无法使用与其父 Freezable 属于不同线程的 DependencyObject

发布于 2024-12-28 23:58:21 字数 985 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2025-01-04 23:58:21

尝试使用表单的 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...

虐人心 2025-01-04 23:58:21

无法使用与其父 Freezable 属于不同线程的 DependencyObject。

出现此错误是因为您尝试使用在 STA 中的不同线程中创建的资源(UIElement 类型)线程(您用来显示弹出窗口)。

在您的情况下,它看起来像第二个线程 Thread th = new Thread(UpdateProgressBar); ,正在尝试操作 WindowBusyPopup 中的 UI。由于弹出窗口由不同的线程拥有,因此您会收到此异常。

可能的解决方案:(据我所知,您没有显示函数 UpdateProgressBar 的实现)

private void UpdateProgressBar()
{
if(_window != null) /* assuming  you declared your window in a scope accesible to this function */
_window.Dispatcher.BeginInvoke(new Action( () => {
// write any code to handle children of window here
}));
}

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)

private void UpdateProgressBar()
{
if(_window != null) /* assuming  you declared your window in a scope accesible to this function */
_window.Dispatcher.BeginInvoke(new Action( () => {
// write any code to handle children of window here
}));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文