C#WPF项目,如何避免出现InvalidOperationException? [英] C# WPF project, how to avoid InvalidOperationException?

查看:652
本文介绍了C#WPF项目,如何避免出现InvalidOperationException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个WPF项目。我得到了以下错误:

We have a WPF project. I got the following error:

InvalidOperationException was unhandled
The calling thread cannot access this object because a different thread owns it.



我试着从我所创建的工作线程创建一个文本框获得价值。

I try to get value from a TextBox from a worker thread created by me.

如何避免这种情况。我能够通过使用委托回拨和()调用的方法来避免这种情况的另一种形式的项目,但不知它不在这个WPF项目'T的工作。

How to avoid this. I was able to avoid this in another Form project by using delegate call back and Invoked() method but somehow it doesn't work in this WPF project.

任何简单的示例代码?谢谢,

any simple sample code? thanks,

推荐答案

如何沿着线的东西:

        if (!Dispatcher.CheckAccess()) 
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            (MyDelegate)delegate
            {
                // Get value
            });
        } 
        else 
        { 
            // Get value
        }

这篇关于C#WPF项目,如何避免出现InvalidOperationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆