locked
How to use PPL asynchronous programming in a Windows Store C++ app?

    Question

  • I am working on a Windows Store (C++) app. This is a method that reads from the database using the web service.
     task<std::wstring> Ternet::GetFromDB(cancellation_token cancellationToken)
        {
        	uriString = ref new String(L"http://myHost:1234/RestServiceImpl.svc/attempt");
        	auto uri = ref new Windows::Foundation::Uri(Helpers::Trim(uriString));
        	cancellationTokenSource = cancellation_token_source();
            return httpRequest.GetAsync(uri, cancellationTokenSource.get_token()).then([this](task<std::wstring> response)->std::wstring
            {
        		try
                {
        			Windows::UI::Popups::MessageDialog wMsg(ref new String(response.get().c_str()), "success");
        			wMsg.ShowAsync();
        			return response.get();
                }
                catch (const task_canceled&)
                {
                    Windows::UI::Popups::MessageDialog wMsg("Couldn't load content. Check internet connectivity.", "Error");
        			wMsg.ShowAsync();
        			std::wstring abc;
        			return abc;
                }
                catch (Exception^ ex)
                {
                    Windows::UI::Popups::MessageDialog wMsg("Couldn't load content. Check internet connectivity.", "Error");
        			wMsg.ShowAsync();
        			std::wstring abc;
        			return abc;
        		}
            } , task_continuation_context::use_current());
        }
    I'm confused how to return the received data to the calling function. Now, I am calling this function in the constructor of my data class like this:
     ternet.GetFromDB(cancellationTokenSource.get_token()).then([this](task<std::wstring> response)
        {
        	data = ref new String(response.get().c_str());
        });
    I am getting a COM exception whenever I try to receive the returned data from GetFromDB(). But this one runs fine:
    ternet.GetFromDB(cancellationTokenSource.get_token());
    Please suggest a better way of chaining the completion of GetFromDB to other code. And how to get the returned value from inside the try{} block of GetFromDB() 's then. Please keep in mind I am a very new student of async programming.
    Thursday, April 4, 2013 7:57 AM

Answers

  • Hi,

     

    You can get the return value from .get(), like this.

           
        }).then([] (task<void> previousTask) 
    	{
    		try
    			{
    				previousTask.get();
    				OutputDebugStringW(L"File created successfully : \n");
    			}
    			catch(Exception^ e)
    			{
    				OutputDebugStringW(L"Failed to create file \n");
    			}
    
    	})

     

    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.

    • Proposed as answer by Jesse Jiang Monday, April 8, 2013 2:38 AM
    • Marked as answer by Jesse Jiang Wednesday, April 10, 2013 1:46 AM
    Friday, April 5, 2013 2:30 AM