locked
Code from MSDN cann't run ?

    Question

  • // lambda-task-lifetime.cpp
    // compile with: /EHsc
    #include <ppltasks.h>
    #include <iostream>
    #include <string>
    
    using namespace concurrency;
    using namespace std;
    
    task<wstring> write_to_string()
    {
        // Create a shared pointer to a string that is 
        // assigned to and read by multiple tasks.
        // By using a shared pointer, the string outlives
        // the tasks, which can run in the background after
        // this function exits.
        auto s = make_shared<wstring>(L"Value 1");
    
        return create_task([s] 
        {
            // Print the current value.
            wcout << L"Current value: " << *s << endl;
            // Assign to a new value.
            *s = L"Value 2";
    
        }).then([s] 
        {
            // Print the current value.
            wcout << L"Current value: " << *s << endl;
            // Assign to a new value and return the string.
            *s = L"Value 3";
            return *s;
        });
    }
    
    int wmain()
    {
        // Create a chain of tasks that work with a string.
        auto t = write_to_string();
    
        // Wait for the tasks to finish and print the result.
        wcout << L"Final value: " << t.get() << endl;
    }
    
    /* Output:
        Current value: Value 1
        Current value: Value 2
        Final value: Value 3
    */

    These code is from MSDN: http://msdn.microsoft.com/en-us/library/windows/apps/dd492427.aspx#lambdas

    I tried, but if failed.

    If I remove next code

    // Wait for the tasks to finish and print the result.
    wcout << L"Final value: " << t.get() << endl;

    So it works OK. Why?

    Need I do something else.

    Notice: My project is BlankApp instead of Win32 Console.



    • Edited by Jumping fish Thursday, November 01, 2012 2:18 AM Need Words
    Thursday, November 01, 2012 1:43 AM

Answers

  • The code you are using is from the desktop documentation and is not designed for use by Windows Store apps.

    You need to modify it to remove the desktop specific parts (such as wcout) to run it in a Windows Store app.

    --Rob

    • Marked as answer by Jesse Jiang Thursday, November 08, 2012 6:10 AM
    Thursday, November 01, 2012 3:00 AM
    Owner

All replies

  • The code you are using is from the desktop documentation and is not designed for use by Windows Store apps.

    You need to modify it to remove the desktop specific parts (such as wcout) to run it in a Windows Store app.

    --Rob

    • Marked as answer by Jesse Jiang Thursday, November 08, 2012 6:10 AM
    Thursday, November 01, 2012 3:00 AM
    Owner
  • I know that I should remove the desktop specific parts (such as wcout).

    And I tried it. It just didn't work.

    Thursday, November 01, 2012 9:52 AM
  • Sorry, in your initial post you said you had it working.

    What does your code look like now that you've modified it to be a Windows Store app rather than a desktop app, and in what way does it not work?

    --Rob

    Thursday, November 01, 2012 4:42 PM
    Owner