[C ++] INotifyPropertyChanged :: PropertyChanged从单独的非UI线程调用时抛出Platform :: WrongThreadException [英] [C++]INotifyPropertyChanged::PropertyChanged throws Platform::WrongThreadException when called from a separate non-UI thread

查看:57
本文介绍了[C ++] INotifyPropertyChanged :: PropertyChanged从单独的非UI线程调用时抛出Platform :: WrongThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现INotifyPropertyChanged的对象:

I have an object that implements INotifyPropertyChanged :

	[Windows::Foundation::Metadata::WebHostHidden]
	public ref class ScreenProjection sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
	{
	public:
		virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        .........

	protected:
		void RaisePropertyChanged(Platform::String ^ prop_name)
		{
                        //it chashes when called from other non-UI thread
                        PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(prop_name));
		}
         };

如果我在单独的非UI线程上创建此对象并更改某些属性,则调用PropertyChanged崩溃,显示以下内容在调试输出中:

if I create this object on a separate non-UI thread and change some property, invocation of PropertyChanged crashes, showing the following in the debug output:

在MyApp.exe中的0x74A93E28(KernelBase.dll)抛出异常:0x40080201:WinRT发起错误(参数:0x8001010E,0x00000051,0x0EAFE45C)。

在GeoViewer.exe中的0x74A93E28处抛出异常:Microsoft C ++异常:Platform :: WrongThreadException ^在内存位置0x0EAFE8FC。 HRESULT:0x8001010E该应用程序调用了一个为不同线程编组的接口。

WinRT信息:该应用程序调用了一个为不同线程编组的接口。

我没有将此对象绑定到UI线程中的某些控件,我不希望它以某种方式通知UI线程,但是我希望它通知一些也在我的创建的对象单独的非UI线程。或者更准确地说我想在UI线程中使用这个
类的一个实例,在非UI线程中使用其他实例。

I did not bind this object to some controls in UI thread and I do not want it to notify UI thread somehow, but I want it to notify some objects that are also created in my separate non-UI thread. Or saying more exactly I want to use one instance of this class in UI thread and other instance in non-UI thread.

所以我的问:这是正常行为还是我的代码有问题?

So my question: is this the normal behavior or something is wrong with my code?

推荐答案

您只能从创建对象的同一个线程上的对象上调用INotifyPropertyChanged :: PropertyChanged。从错误消息中,听起来你在线程'X'上创建一个ScreenProjection对象然后试图从线程'Y'调用
它的RaisePropertyChanged方法。

You can only call INotifyPropertyChanged::PropertyChanged on an object from the same thread that the object was created on. From the error message, it sounds like you're creating a ScreenProjection object on thread 'X' and then later are trying to call its RaisePropertyChanged method from thread 'Y'.

有关从C ++角度看Windows Runtime线程模型的更多信息,请阅读:

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771042.aspx

For more on the Windows Runtime threading model from a C++ perspective, read this: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771042.aspx .


这篇关于[C ++] INotifyPropertyChanged :: PropertyChanged从单独的非UI线程调用时抛出Platform :: WrongThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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