// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pch.h" #include "AsyncHelper.h" #include #include using namespace std; using namespace concurrency; using namespace Platform; using namespace CalculatorApp; using namespace Windows::UI::Core; using namespace Windows::ApplicationModel::Core; task AsyncHelper::RunOnUIThreadAsync(function&& action) { auto callback = ref new DispatchedHandler([action]() { action(); }); return create_task(CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, callback)); } void AsyncHelper::RunOnUIThread(function&& action, DWORD timeout) { task waitTask = RunOnUIThreadAsync([action]() { action(); }); WaitForTask(waitTask, timeout); } void AsyncHelper::Delay(DWORD milliseconds) { thread timer(bind(CalculatorApp::AsyncHelper::Sleep, milliseconds)); timer.join(); } void AsyncHelper::Sleep(DWORD milliseconds) { this_thread::sleep_for(chrono::milliseconds(milliseconds)); }