locked
Exposing an IAsyncAction which completes when an event occurs

    Question

  • I want to have a method in a WinRT component which starts playing a sound and returns an IAsyncAction to enable monitoring the completion of the sound. Something like:

    IAsyncAction^ Play(Sound^ sound);

    I know that the typical way to expose an IAsync*** from C++ is to use create_async, however in this case I am not performing any kind of computation, I'm waiting for a callback to be invoked (by XAudio2). I could use create_async and block inside the lambda on some event that will be triggered by the sound completion callback, but that would mean stalling one thread per sound being played and Windows 8 Metro Modern UI Store Apps (or whatever they're called) are supposed to be all about proper usage of resources.

    I've also had a look at implementing IAsyncAction myself but the presence of the IAsyncInfo::Id property and the way it's implemented in ppl tasks hints that it's not something that's supported.

    There doesn't seem to be a way to create an IAsyncAction from a task_completion_source either.

    Any ideas?

    Friday, September 07, 2012 2:59 PM

Answers

  • You can indeed create an IAsyncAction from a task_completion_event:

    task_completion_event<...> tce;
    auto asyncOp = create_async([=](){
        return create_task(tce);
    });
    • Marked as answer by Jesse Jiang Thursday, September 13, 2012 4:54 AM
    Monday, September 10, 2012 8:28 PM