AsyncHelper.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "windows" -*- */
  2. //
  3. // AsyncHelper.h: UWP support library for SDLPal.
  4. //
  5. // Original author: David Huang @ 1st July, 2014
  6. // (C) 2014 Light Studio. All Rights Reserved.
  7. //
  8. // Modified by: Lou Yihua @ 2016
  9. //
  10. // Copyright (c) 2016-2017 SDLPAL development team.
  11. // All rights reserved.
  12. //
  13. // This file is part of SDLPAL.
  14. //
  15. // SDLPAL is free software: you can redistribute it and/or modify
  16. // it under the terms of the GNU General Public License as published by
  17. // the Free Software Foundation, either version 3 of the License, or
  18. // (at your option) any later version.
  19. //
  20. // This program is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. // GNU General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU General Public License
  26. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. //
  28. #pragma once
  29. #include <ppltasks.h>
  30. #include <Windows.h>
  31. #include <synchapi.h>
  32. using namespace Platform;
  33. template <typename TResult>
  34. inline TResult AWait(Windows::Foundation::IAsyncOperation<TResult>^ asyncOp, HANDLE eventHandle)
  35. {
  36. TResult result;
  37. Exception^ exceptionObj = nullptr;
  38. ResetEvent(eventHandle);
  39. asyncOp->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<TResult>([&]
  40. (Windows::Foundation::IAsyncOperation<TResult>^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  41. {
  42. try
  43. {
  44. result = asyncInfo->GetResults();
  45. }
  46. catch (Exception^ e)
  47. {
  48. exceptionObj = e;
  49. }
  50. SetEvent(eventHandle);
  51. });
  52. WaitForSingleObjectEx(eventHandle, INFINITE, FALSE);
  53. if (exceptionObj != nullptr) throw exceptionObj;
  54. return result;
  55. }
  56. template <typename TResult, typename TProgress>
  57. inline TResult AWait(Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncOp, HANDLE eventHandle)
  58. {
  59. TResult result;
  60. Exception^ exceptionObj = nullptr;
  61. ResetEvent(eventHandle);
  62. asyncOp->Completed = ref new Windows::Foundation::AsyncOperationWithProgressCompletedHandler<TResult, TProgress>([&]
  63. (Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  64. {
  65. try
  66. {
  67. result = asyncInfo->GetResults();
  68. }
  69. catch (Exception^ e)
  70. {
  71. exceptionObj = e;
  72. }
  73. SetEvent(eventHandle);
  74. });
  75. WaitForSingleObjectEx(eventHandle, INFINITE, FALSE);
  76. if (exceptionObj != nullptr) throw exceptionObj;
  77. return result;
  78. }
  79. inline void AWait(Windows::Foundation::IAsyncAction^ asyncAc, HANDLE eventHandle)
  80. {
  81. Exception^ exceptionObj = nullptr;
  82. asyncAc->Completed = ref new Windows::Foundation::AsyncActionCompletedHandler([&]
  83. (Windows::Foundation::IAsyncAction^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  84. {
  85. try
  86. {
  87. asyncInfo->GetResults();
  88. }
  89. catch (Exception^ e)
  90. {
  91. exceptionObj = e;
  92. }
  93. SetEvent(eventHandle);
  94. });
  95. WaitForSingleObjectEx(eventHandle, INFINITE, FALSE);
  96. if (exceptionObj != nullptr) throw exceptionObj;
  97. }
  98. template <typename TResult>
  99. inline TResult AWait(Windows::Foundation::IAsyncOperation<TResult>^ asyncOp)
  100. {
  101. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  102. auto result = AWait(asyncOp, handle);
  103. CloseHandle(handle);
  104. return result;
  105. }
  106. template <typename TResult, typename TProgress>
  107. inline TResult AWait(Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncOp)
  108. {
  109. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  110. auto result = AWait(asyncOp, handle);
  111. CloseHandle(handle);
  112. return result;
  113. }
  114. inline void AWait(Windows::Foundation::IAsyncAction^ asyncAc)
  115. {
  116. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  117. AWait(asyncAc, handle);
  118. CloseHandle(handle);
  119. }