1
0

MainThread.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef NULLSOFT_MAINTHREADH
  2. #define NULLSOFT_MAINTHREADH
  3. #include <windows.h>
  4. class Lambda
  5. {
  6. public:
  7. virtual void Run() = 0;
  8. };
  9. template <class func_t, class param_t>
  10. class LambdaC : public Lambda
  11. {
  12. public:
  13. LambdaC(func_t _func, param_t _param)
  14. : func(_func), param(_param)
  15. {
  16. event = CreateEvent(0, FALSE, FALSE, 0);
  17. }
  18. ~LambdaC()
  19. {
  20. CloseHandle(event);
  21. }
  22. void Run()
  23. {
  24. func(param);
  25. SetEvent(event);
  26. }
  27. private:
  28. HANDLE event;
  29. func_t func;
  30. param_t param;
  31. };
  32. template <class object_t, class func_t, class param_t>
  33. class LambdaCPP
  34. {
  35. public:
  36. LambdaCPP(object_t *_object, func_t *_func, param_t _param)
  37. : func(_func), param(_param)
  38. {
  39. event = CreateEvent(0, FALSE, FALSE, 0);
  40. }
  41. ~LambdaCPP()
  42. {
  43. CloseHandle(event);
  44. }
  45. void Run()
  46. {
  47. object->*func(param);
  48. SetEvent(event);
  49. }
  50. private:
  51. HANDLE event;
  52. object_t *object;
  53. func_t *func;
  54. param_t param;
  55. };
  56. class MainThread
  57. {
  58. public:
  59. MainThread();
  60. template <class func_t, class param_t>
  61. void Run(func_t *func, param_t param)
  62. {
  63. Lambda *lambda = new LambdaC(func, param);
  64. PostMessage(mainWindow, WM_USER, lambda, 0);
  65. }
  66. template <class object_t, class func_t, class param_t>
  67. void Run(object_t *object, func_t *func, param_t param)
  68. {
  69. Lambda *lambda = new LambdaCPP(object, func, param);
  70. PostMessage(mainWindow, WM_USER, lambda, 0);
  71. }
  72. private:
  73. HWND mainWindow;
  74. };
  75. extern MainThread mainThread;
  76. #endif