locked
WinRT component and event

    Question

  • I have a WinRT C++ component which work fine when use in C#.
    (implementation below)

    However, when I tried to use it in a Direct3D C++ WinRT app it crashes in the constructor, while calling
    ref new PerformanceTimer()
    -> Reset()
    -> OnPropertyChanged("Total")
    -> PropertyChanged(this, ....)

    I got a "Platform::WrongThreadException ^"

    What can I do?
    I need those event to notify about some important change (this Total property is not important, but others are)

    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class PerformanceTimer sealed :  Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    public:
    	PerformanceTimer()
    	{
    		Reset();
    	}
    	
    	void Reset()
    	{
    		m_total = 0.0f;
    		OnPropertyChanged("Total");
    	}
    
    	property float Total { float get() { return m_total; } }
    	
    
    #pragma region INotifyPropertyChanged
        private:
            void OnPropertyChanged(Platform::String^ propertyName)
            {
    			PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
            }
    
    		event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ _pChanged;
        public:
    		virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
    		{
    			Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ value)
    			{
    				return _pChanged::add(value);
    			}
    			void remove(Windows::Foundation::EventRegistrationToken token)
    			{
    				_pChanged::remove(token);
    			}
    		internal:
    			void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
    			{
    				_pChanged::raise(sender, e);
    			}
    		}
    #pragma endregion	
    
    private:
    	LARGE_INTEGER m_frequency;
    
    	float m_total;
    };
    

    Tuesday, December 11, 2012 3:49 PM

All replies

  • Hi,

    How about remove the add, remove and raise function?
    I think we should write these function in this way.

                // in c++, it is not neccessary to include definitions of add, remove, and raise.
                //  these definitions have been made explicitly here so that we can check if the 
                //  event has listeners before firing the event
                virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
                {
                    virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
                    {
                        _isPropertyChangedObserved = true;
                        return _privatePropertyChanged += e;
                    }
                    virtual void remove(Windows::Foundation::EventRegistrationToken t)
                    {
                        _privatePropertyChanged -= t;
                    }
    
            protected:
                    virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
                    {
                        if (_isPropertyChangedObserved)
                        {
                            _privatePropertyChanged(sender, e);
                        }
                    }
                }

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 13, 2012 6:42 AM
  • Hi Jess, indeed I was confused at some stage by some compilation error (C++ is a bit tough on newbie with kilometers of error for a misspell.. :~)

    As it turns out I don't need explicit definition of the event at all!
    But this was instructive! :)

    Anyway, as it is the problem remains....
    I have a sample at this location:

    https://skydrive.live.com/redir?resid=7E5D519CDA2F47D0!1752

    Everything seems to work ok, but it's because in the C++ sample (Sample0_DXCoreWindow) I worked around 2 problems:

    - use the event by modifying
    DXBase\Scene.cpp:26, remove "if(w)" so that the event handler is always added => crash

    - fire event even without listener by modifying
    DXBase\PerformanceTimer.h:103 use the PROPERTY_CHANGE_EVENT_AUTO macro (instead of PROPERTY_CHANGE_EVENT_EXPLICIT) => app crash

    Thursday, December 13, 2012 10:52 AM