#pragma once
#include "pch.h"
#include <ppl.h>
#include <ppltasks.h>
#include <wrl.h>
#include <wrl/implements.h>
#include "Common/Log.h"
#include "UWPUtil.h"
using namespace Windows::UI::Core;
#pragma region Async Handlers
template<typename T>
T TaskHandler(std::function<concurrency::task<T>()> wtask, T def)
{
T result = def;
bool done = false;
wtask().then([&](concurrency::task<T> t) {
try
{
result = t.get();
}
catch (Platform::Exception^ exception_)
{
ERROR_LOG(Log::FileSystem, FromPlatformString(exception_->Message).c_str());
}
done = true;
});
CoreWindow^ corewindow = CoreWindow::GetForCurrentThread();
while (!done)
{
try {
if (corewindow) {
corewindow->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
else {
corewindow = CoreWindow::GetForCurrentThread();
}
}
catch (...) {
}
}
return result;
};
template<typename T>
T TaskPass(Windows::Foundation::IAsyncOperation<T>^ task, T def)
{
return TaskHandler<T>([&]() {
return concurrency::create_task(task).then([](T res) {
return res;
});
}, def);
}
bool ActionPass(Windows::Foundation::IAsyncAction^ action);
#pragma endregion
template<typename T>
void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task)
{
try {
out = TaskPass<T>(task, T());
}
catch (...) {
out = T();
}
};
template<typename T>
void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task, T def)
{
try{
out = TaskPass<T>(task, def);
}
catch (...) {
out = def;
}
};
bool ExecuteTask(Windows::Foundation::IAsyncAction^ action);