C# 解决报错:调用线程无法访问此对象,因为另一个线程拥有该对象。

在给桌面控件赋值是,由于线程问题,会导致如下报错:

调用线程无法访问此对象,因为另一个线程拥有该对象。解决办法如下:

控件名称.Dispatcher.Invoke(new Action(() => { 控件名称.属性 = 值 }));

如:

this.PageIndex.Dispatcher.Invoke(new Action(() => { this.PageIndex.Content = 8; }));

 Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    this.PageIndex.Content = 666;
                }),
                    DispatcherPriority.ApplicationIdle);

 

你可能感兴趣的:(程序开发,笔记,c#)