AsyncHelper.h 3.8 KB

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