Let's say I have a function that I call on the UI thread:
IAsyncOperation<BitmapImage^>^ GetImage()
{
return create_async([]
{
return create_task(GetStreamAsync())
.then([] (Stream^ stream)
{
auto image = ref new BitmapImage();
// set image properties.
image.SetSourceAsync(stream);
return image;
});
});
}
I know the inner task will also execute on the UI thread since this question's answer indicates an apartment aware task continues in its apartment by default.
But when is the execution of the create_task body scheduled? Does it end up happening synchronously? Or does the UI thread wait until it has free cycles to process the async logic?
Let's find out:
This shows something like this:
You can see they're all on the same thread -
create_asynccalls its argument immediately (it doesn't schedule it as a task) and simply converts the return value to anIAsyncOperation<T>. If you want the continuation to not be on the UI thread, you can do this:Then PPL will run the continuation on an arbitrary thread.