CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UWP/UWPHelpers/StorageAsync.h
Views: 1401
// Thanks to RetroArch/Libretro team for this idea1// This is improved version of the original idea23#pragma once45#include "pch.h"6#include <ppl.h>7#include <ppltasks.h>8#include <wrl.h>9#include <wrl/implements.h>1011#include "Common/Log.h"12#include "UWPUtil.h"1314using namespace Windows::UI::Core;1516// Don't add 'using' 'Windows::Foundation'17// it might cause confilct with some types like 'Point'1819#pragma region Async Handlers2021template<typename T>22T TaskHandler(std::function<concurrency::task<T>()> wtask, T def)23{24T result = def;25bool done = false;26wtask().then([&](concurrency::task<T> t) {27try28{29result = t.get();30}31catch (Platform::Exception^ exception_)32{33ERROR_LOG(Log::FileSystem, FromPlatformString(exception_->Message).c_str());34}35done = true;36});3738CoreWindow^ corewindow = CoreWindow::GetForCurrentThread();39while (!done)40{41try {42if (corewindow) {43corewindow->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);44}45else {46corewindow = CoreWindow::GetForCurrentThread();47}48}49catch (...) {5051}52}5354return result;55};5657template<typename T>58T TaskPass(Windows::Foundation::IAsyncOperation<T>^ task, T def)59{60return TaskHandler<T>([&]() {61return concurrency::create_task(task).then([](T res) {62return res;63});64}, def);65}6667bool ActionPass(Windows::Foundation::IAsyncAction^ action);6869#pragma endregion7071// Now it's more simple to execute async task72// @out: output variable73// @task: async task74template<typename T>75void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task)76{77try {78out = TaskPass<T>(task, T());79}80catch (...) {81out = T();82}83};8485// For specific return default value86// @out: output variable87// @task: async task88// @def: default value when fail89template<typename T>90void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task, T def)91{92try{93out = TaskPass<T>(task, def);94}95catch (...) {96out = def;97}98};99100101// Async action such as 'Delete' file102// @action: async action103// return false when action failed104bool ExecuteTask(Windows::Foundation::IAsyncAction^ action);105106107