locked
How can i not async function as sync function

    Question

  • Just like the title,I don't want to use async sometimes,I want to sync the operations.which way can i use to do this?
    Monday, April 16, 2012 2:49 AM

Answers

  • What sort of exception do you get?

    Are you trying to run this synchronously on the UI thread or on a worker thread?

    If on the UI thread then please don't do that. Blocking the UI thread is very unfriendly to your users and Task.wait() will raise an exception if called on a UI (STA) thread.

    While you can block the UI thread through other means, it is much better to move the lengthy operation to a worker thread. The async system is designed to do this easily.

    See Asynchronous programming in C++

    --Rob

    • Marked as answer by OceanCat Thursday, April 19, 2012 4:01 AM
    Thursday, April 19, 2012 2:27 AM
    Owner

All replies

  • Hello,

    I think you can wrapper the async function, using .wait() like:

    int getvalue()
    {
        task<int> t([]() {
            return 42;
        });
    
        t.wait();
        return t.get();
    }

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 17, 2012 5:38 AM
  • Thanks for reply,but I got an UnhandledException when I run you code in a metro app.I just copy and paste your function.May be you can try to test the code by yourself. If I misunderstood something,please tell me.


    Thursday, April 19, 2012 1:51 AM
  • What sort of exception do you get?

    Are you trying to run this synchronously on the UI thread or on a worker thread?

    If on the UI thread then please don't do that. Blocking the UI thread is very unfriendly to your users and Task.wait() will raise an exception if called on a UI (STA) thread.

    While you can block the UI thread through other means, it is much better to move the lengthy operation to a worker thread. The async system is designed to do this easily.

    See Asynchronous programming in C++

    --Rob

    • Marked as answer by OceanCat Thursday, April 19, 2012 4:01 AM
    Thursday, April 19, 2012 2:27 AM
    Owner
  • May be I have got the answer.I run it on the UI thread.I will try you suggest later,Thank you very much:-)
    Thursday, April 19, 2012 3:35 AM