c++ - Analyze concurency::task() API & why do we need this? -
i'm, trying understand syntax of concurrency::task in below code snippet.
i'm unable understand code snippet syntax. how analyze this:
what "getfileoperation" here. object of type storagefile class ? "then" keyword mean here ? there "{" after then(....)? i'm unable analyze syntax ?
also why need concurrency::task().then().. use case ?
concurrency::task<windows::storage::storagefile^> getfileoperation(installfolder->getfileasync("images\\test.png")); getfileoperation.then([](windows::storage::storagefile^ file) { if (file != nullptr) {
taken msdn concurrency::task api
void mainpage::defaultlaunch() { auto installfolder = windows::applicationmodel::package::current->installedlocation; concurrency::task<windows::storage::storagefile^> getfileoperation(installfolder->getfileasync("images\\test.png")); getfileoperation.then([](windows::storage::storagefile^ file) { if (file != nullptr) { // set option show picker auto launchoptions = ref new windows::system::launcheroptions(); launchoptions->displayapplicationpicker = true; // launch retrieved file concurrency::task<bool> launchfileoperation(windows::system::launcher::launchfileasync(file, launchoptions)); launchfileoperation.then([](bool success) { if (success) { // file launched } else { // file launch failed } }); } else { // not find file } }); }
getfileoperation
object return storagefile^
(or error) @ point in future. c++ task<t>
wrapper around winrt iasyncoperation<t>
object returned getfileasync
.
the implementation of getfileasync
can (but isn't required to) execute on different thread, allowing calling thread continue doing other work (like animating ui or responding user input).
the then
method lets pass continuation function called once asynchronous operation has completed. in case, you're passing lambda (inline anonymous function) identified []
square brackets followed lambda parameter list (a storagefile^
, object returned getfileasync
) , function body. function body executed once getfileasync
operation completes work sometime in future.
the code inside continuation function passed then
typically (but not always) executes after code follows call create_task()
(or in case task
constructor).
Comments
Post a Comment