AsyncHelper.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * AsyncHelper.h
  3. *
  4. * Date: 1st July, 2014 Author: David Huang
  5. * (C) 2014 Light Studio. All Rights Reserved.
  6. * Modified by louyihua@2016
  7. */
  8. #pragma once
  9. #include <ppltasks.h>
  10. #include <Windows.h>
  11. #include <synchapi.h>
  12. using namespace Platform;
  13. template <typename TResult>
  14. inline TResult AWait(Windows::Foundation::IAsyncOperation<TResult>^ asyncOp, HANDLE eventHandle)
  15. {
  16. TResult result;
  17. Exception^ exceptionObj = nullptr;
  18. ResetEvent(eventHandle);
  19. asyncOp->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<TResult>([&]
  20. (Windows::Foundation::IAsyncOperation<TResult>^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  21. {
  22. try
  23. {
  24. result = asyncInfo->GetResults();
  25. }
  26. catch (Exception^ e)
  27. {
  28. exceptionObj = e;
  29. }
  30. SetEvent(eventHandle);
  31. });
  32. WaitForSingleObjectEx(eventHandle, INFINITE, FALSE);
  33. if (exceptionObj != nullptr) throw exceptionObj;
  34. return result;
  35. }
  36. template <typename TResult, typename TProgress>
  37. inline TResult AWait(Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncOp, HANDLE eventHandle)
  38. {
  39. TResult result;
  40. Exception^ exceptionObj = nullptr;
  41. ResetEvent(eventHandle);
  42. asyncOp->Completed = ref new Windows::Foundation::AsyncOperationWithProgressCompletedHandler<TResult, TProgress>([&]
  43. (Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  44. {
  45. try
  46. {
  47. result = asyncInfo->GetResults();
  48. }
  49. catch (Exception^ e)
  50. {
  51. exceptionObj = e;
  52. }
  53. SetEvent(eventHandle);
  54. });
  55. WaitForSingleObjectEx(eventHandle, INFINITE, FALSE);
  56. if (exceptionObj != nullptr) throw exceptionObj;
  57. return result;
  58. }
  59. inline void AWait(Windows::Foundation::IAsyncAction^ asyncAc, HANDLE eventHandle)
  60. {
  61. Exception^ exceptionObj = nullptr;
  62. asyncAc->Completed = ref new Windows::Foundation::AsyncActionCompletedHandler([&]
  63. (Windows::Foundation::IAsyncAction^ asyncInfo, Windows::Foundation::AsyncStatus asyncStatus)
  64. {
  65. try
  66. {
  67. 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. }
  78. template <typename TResult>
  79. inline TResult AWait(Windows::Foundation::IAsyncOperation<TResult>^ asyncOp)
  80. {
  81. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  82. auto result = AWait(asyncOp, handle);
  83. CloseHandle(handle);
  84. return result;
  85. }
  86. template <typename TResult, typename TProgress>
  87. inline TResult AWait(Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>^ asyncOp)
  88. {
  89. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  90. auto result = AWait(asyncOp, handle);
  91. CloseHandle(handle);
  92. return result;
  93. }
  94. inline void AWait(Windows::Foundation::IAsyncAction^ asyncAc)
  95. {
  96. HANDLE handle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  97. AWait(asyncAc, handle);
  98. CloseHandle(handle);
  99. }