MT5APIThread.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //+------------------------------------------------------------------+
  2. //| MetaTrader 5 |
  3. //| Copyright 2000-2019, MetaQuotes Software Corp. |
  4. //| http://www.metaquotes.net |
  5. //+------------------------------------------------------------------+
  6. #pragma once
  7. #include <process.h>
  8. //+------------------------------------------------------------------+
  9. //| Thread manipulation class |
  10. //+------------------------------------------------------------------+
  11. class CMTThread
  12. {
  13. private:
  14. HANDLE m_thread;
  15. public:
  16. CMTThread(void);
  17. ~CMTThread(void);
  18. //---
  19. inline bool Start(unsigned (__stdcall *thread_func)(void*),void *thread_param,const UINT stack_size);
  20. inline bool Shutdown(const UINT timeout=INFINITE);
  21. inline void Terminate(void);
  22. inline bool IsBusy(void);
  23. inline HANDLE Handle(void) const { return(m_thread); }
  24. inline bool Priority(int priority);
  25. };
  26. //+------------------------------------------------------------------+
  27. //| Constructor |
  28. //+------------------------------------------------------------------+
  29. inline CMTThread::CMTThread(void) : m_thread(NULL)
  30. {
  31. }
  32. //+------------------------------------------------------------------+
  33. //| Destructor |
  34. //+------------------------------------------------------------------+
  35. inline CMTThread::~CMTThread(void)
  36. {
  37. //--- wait for shutdown
  38. Shutdown();
  39. //--- close handle
  40. if(m_thread!=NULL)
  41. {
  42. CloseHandle(m_thread);
  43. m_thread=NULL;
  44. }
  45. }
  46. //+------------------------------------------------------------------+
  47. //| Thread start |
  48. //+------------------------------------------------------------------+
  49. inline bool CMTThread::Start(unsigned (__stdcall *thread_func)(void*),void *thread_param,const UINT stack_size)
  50. {
  51. DWORD id=0;
  52. //--- thread has been started...
  53. if(m_thread!=NULL)
  54. {
  55. GetExitCodeThread(m_thread,&id);
  56. //--- still active
  57. if(id==STILL_ACTIVE)
  58. return(false);
  59. //--- close handle
  60. CloseHandle(m_thread);
  61. m_thread=NULL;
  62. }
  63. //--- start thread
  64. if((m_thread=(HANDLE)_beginthreadex(NULL,stack_size,thread_func,(void*)thread_param,STACK_SIZE_PARAM_IS_A_RESERVATION,(UINT*)&id))==NULL)
  65. return(false);
  66. //--- ok
  67. return(true);
  68. }
  69. //+------------------------------------------------------------------+
  70. //| Thread shutdown |
  71. //+------------------------------------------------------------------+
  72. inline bool CMTThread::Shutdown(const UINT timeout/*=INIFINITE*/)
  73. {
  74. //--- check
  75. if(m_thread==NULL)
  76. return(true);
  77. //--- wait for thread shutdown
  78. return(WaitForSingleObject(m_thread,timeout)==WAIT_OBJECT_0);
  79. }
  80. //+------------------------------------------------------------------+
  81. //| Thread termination |
  82. //+------------------------------------------------------------------+
  83. inline void CMTThread::Terminate(void)
  84. {
  85. //--- check
  86. if(m_thread)
  87. {
  88. //--- kill thread
  89. TerminateThread(m_thread,0);
  90. //--- close handle
  91. CloseHandle(m_thread);
  92. m_thread=NULL;
  93. }
  94. //---
  95. }
  96. //+------------------------------------------------------------------+
  97. //| Check thread activity |
  98. //+------------------------------------------------------------------+
  99. inline bool CMTThread::IsBusy(void)
  100. {
  101. //--- check
  102. if(!m_thread)
  103. return(false);
  104. //--- request state
  105. DWORD res=0;
  106. GetExitCodeThread(m_thread,&res);
  107. if(res==STILL_ACTIVE)
  108. return(true);
  109. //--- close handle
  110. CloseHandle(m_thread);
  111. m_thread=NULL;
  112. //--- thread finished
  113. return(false);
  114. }
  115. //+------------------------------------------------------------------+
  116. //| Thread priority modification |
  117. //+------------------------------------------------------------------+
  118. inline bool CMTThread::Priority(int priority)
  119. {
  120. return(m_thread && SetThreadPriority(m_thread,priority));
  121. }
  122. //+------------------------------------------------------------------+