none
UWP(C++/CX )异步编程计算的值不能立即读取,在消息响应函数中可以读取 RRS feed

  • 问题

  • UWP(C++/CX )异步编程计算的值不能立即读取,但是在消息响应函数中可以读取.
    <Page
        x:Class="CreateTask.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:CreateTask"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Button Content="ceshi" Click="Click_Button"/>
        </Grid>
    </Page>
    
    
    
    //
    // MainPage.xaml.h
    // MainPage 类的声明。
    //
    
    #pragma once
    
    #include "MainPage.g.h"
    #include <ppltasks.h>
    #include "AsyncTask.h"
    using namespace Windows::ApplicationModel::Resources::Core;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Platform;
    using namespace Windows::UI::Xaml::Interop;
    
    using namespace concurrency;
    using namespace Windows::UI::Xaml::Interop;
    namespace CreateTask
    {
    	/// <summary>
    	/// 可用于自身或导航至 Frame 内部的空白页。
    	/// </summary>
    	public ref class MainPage sealed
    	{
    	public:
    		MainPage();
    	private:
    		AsyncTask^ asyncTask;
    	private:
    		void Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
    	};
    }
    
    
    
    //
    // MainPage.xaml.cpp
    // MainPage 类的实现。
    //
    
    #include "pch.h"
    #include "MainPage.xaml.h"
    #include "AsyncTask.h"
    using namespace CreateTask;
    
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    
    using namespace Windows::UI::Xaml::Interop;
    using namespace Windows::UI::Popups;
    //“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍
    
    MainPage::MainPage()
    {
    	InitializeComponent();
    	asyncTask = ref new AsyncTask();
    	MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
    	msg->ShowAsync();
    }
    
    
    
    void CreateTask::MainPage::Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
    	msg->ShowAsync();
    }
    
    
    
    #pragma once
    using namespace Windows::ApplicationModel::Resources::Core;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Platform;
    using namespace Windows::UI::Xaml::Interop;
    
    using namespace concurrency;
    
    namespace CreateTask
    {
    	public ref class AsyncTask sealed : INotifyPropertyChanged
    	{
    	public:
    		AsyncTask();
    		void InitTask();
    		void ShowN();
    		IAsyncOperation<float64>^ GetPrimesAsync(float64 x, float64 y);
    	private:
    		float64 _n;
    		bool _isPropertyChangedObserved;
    		event PropertyChangedEventHandler^ _privatePropertyChanged;
    	protected:
    		void OnPropertyChanged(Platform::String^ propertyName)
    		{
    			if (_isPropertyChangedObserved)
    			{
    				PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
    			}
    		}
    	public:
    		property float64 N
    		{
    			float64 get()
    			{
    				return _n;
    			}
    
    			void set(float64 value)
    			{
    				_n = value;
    				OnPropertyChanged("N");
    			}
    		}
    		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);
    				}
    			}
    		}
    	};
    }
    
    
    
    
    #include "pch.h"
    #include "AsyncTask.h"
    
    using namespace CreateTask;
    
    using namespace Windows::ApplicationModel::Resources::Core;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Platform;
    using namespace Windows::UI::Xaml::Interop;
    
    using namespace concurrency;
    using namespace Windows::UI::Popups;
    
    AsyncTask::AsyncTask()
    {
    	InitTask();
    	
    }
    
    void AsyncTask::InitTask()
    {
    	create_task(GetPrimesAsync(111.1, 222.2)).then(
    		[this](float64 z)
    
    	{	
    		N = z;
    	});
    }
    
    IAsyncOperation<float64>^ AsyncTask::GetPrimesAsync(float64 x, float64 y)
    {
    	return create_async([this, x, y]() -> float64
    	{
    		// Ensure that the input values are in range. 
    		float64 z;
    		z = x + y;
    
    		return z;
    	});
    }
    
    void AsyncTask::ShowN()
    {
    	MessageDialog^ msg = ref new MessageDialog(N.ToString());
    	msg->ShowAsync();
    }
    
    

    2016年12月6日 8:05