Creating task in C++/UWP

155 Views Asked by At

I'm trying to create a task in VS2019 UWP C++. I've got this bit of code:

IAsyncAction ^frame;
auto frame_task = create_task(frame);
frame_task.then([this]()
    {
        // Noop
    });

What I want to do is have an infinite loop that does frame processing periodically where the Noop is. This code compiles but throws and exception claiming this is NULL. Perhaps I'm not providing a return value for the lambda? Any suggestions welcome. Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

It's not clear what the IAsyncAction is for in that context. If you're using the code as is, the exception is to be thrown because the "frame" is nullptr. If you just want to create a simple task, put the lambda expression directly in create_task().

auto frame_task = create_task([this]()
{
    // Noop
});

... Or otherwise, if you need to obtain the IAsyncAction too, use create_async() instead and wrap it with a task.

IAsyncAction ^frame = create_async([this]()
{
    // Noop
});
auto frame_task = create_task(frame);