locked
C++ CX Windows RT create_task working with returns vs C# Confusion

    Question

  • Firstly apologies if this isn't the right forum and secondly if I am totally missing the point.

    How do I achieve something as simple as the C# code sample below in C++ CX without using a static variable which of course if horrible?


    C#

    var folder = awaitWindows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
    var file = await folder.GetFileAsync("customTile.xml");
    string szCustomTileXML = await Windows.Storage.FileIO.ReadTextAsync(file);
    HttpClient c = new HttpClient();
    await var s = c->GetStringAsync(new Uri("www.bing.com"));
    Border tile = XamlReader.Load(szCustomTileXML) as Border;
    // Take http data, split it and using 'tile' set some TextBlocks

    C++ CX 

    static String^ markup = ref new String();
    
        return create_task(Package::Current->InstalledLocation->GetFolderAsync("Assets"))
        .then([inputMarkupFilename](StorageFolder^ assetsFolder) ->IAsyncOperation<StorageFile^>^ 
        {
            return assetsFolder->GetFileAsync(inputMarkupFilename);
        }
        ).then([](StorageFile^ markupStorageFile)  ->IAsyncOperation<Platform::String^>^ 
        {
            return FileIO::ReadTextAsync(markupStorageFile);
        }
        //).then([this, outputImageFilename, size](Platform::String^ markupContent)
        ).then([this, outputImageFilename, size](Platform::String^ markupContent) -> Windows::Foundation::IAsyncOperationWithProgress<Platform::String^, Windows::Web::Http::HttpProgress>^
        {
            markup = markupContent;
    
            HttpClient ^hc = ref new HttpClient();
    
            return hc->GetStringAsync(ref new Uri("www.bing.com"));
        }
        ).then([this, outputImageFilename, size](Platform::String^ httpContent) 
        {
            Border^ border = (Border^)XamlReader::Load(markup);
    
    // Take http data, split it and using 'tile' set some TextBlocks
    // return ...
    
    });


    Friday, March 20, 2015 8:26 PM

All replies

  • you need to capture a variable by reference

    http://en.cppreference.com/w/cpp/language/lambda

    And for example

    String^ value=nullptr;
    
    create_task(MyAsyncMethod()).then([&value](T^ asyncResult)
    {
       value="this is the information";
    });

    Sunday, March 22, 2015 2:40 PM
  • Funny, I had tried this and all combinations before (or I thought I had) and I was still getting issues. 

    I will take another look this week and get back to you to the forum, thanks.

    Monday, March 23, 2015 7:23 PM
  • Just a side note, since you have multiple then,

    Capture the variable in everyone of them until it reaches the one to be used.

    Tuesday, March 24, 2015 12:37 AM