Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.9 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 03/09/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Thread1.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Thread1.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include <process.h>
  16. #include "Thread.h"
  17. CThread::CThread()
  18. {
  19. // erase all data
  20. m_dwThreadId = 0UL;
  21. m_hEventStopThread = NULL;
  22. m_hThread = NULL;
  23. m_bSelfDestroy = FALSE;
  24. }
  25. CThread::~CThread()
  26. {
  27. // if it is not self destroy
  28. if( !m_bSelfDestroy )
  29. Stop( TRUE );
  30. }
  31. BOOL CThread::Start( BOOL bSelfDestroy )
  32. {
  33. // blocking stop if thread was ran before
  34. if( !Stop( TRUE ) )
  35. return FALSE;
  36. // store for future use
  37. m_bSelfDestroy = bSelfDestroy;
  38. // create event for thread's stopping
  39. m_hEventStopThread = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  40. if( !m_hEventStopThread )
  41. return FALSE;
  42. // create new thread
  43. m_hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, this, 0, (PUINT)&m_dwThreadId );
  44. if( !m_hThread )
  45. return FALSE;
  46. // all work was done successefully
  47. return TRUE;
  48. }
  49. BOOL CThread::Stop( BOOL bWaitForComplete )
  50. {
  51. // force thread to close
  52. if( m_hEventStopThread )
  53. ::SetEvent( m_hEventStopThread );
  54. // if don't need to wait until thread will closed
  55. if( !bWaitForComplete )
  56. return TRUE;
  57. // prevent dead lock if tryed to close from thread itself
  58. if( m_dwThreadId == ::GetCurrentThreadId() )
  59. return FALSE;
  60. // assume success
  61. BOOL bResult = TRUE;
  62. // if thread exists and works
  63. if( m_hThread )
  64. {
  65. // wait until thread will closed
  66. bResult = ::WaitForSingleObject( m_hThread, MAX_THREAD_CLOSE_TIME ) == WAIT_OBJECT_0;
  67. // if thread doesn't closed normally
  68. if( !bResult )
  69. _endthreadex( (UINT)m_hThread );
  70. // close thread handle
  71. ::CloseHandle( m_hThread );
  72. // erase old value
  73. m_hThread = NULL;
  74. }
  75. // if event handle is valid
  76. if( m_hEventStopThread )
  77. {
  78. // close event handle
  79. ::CloseHandle( m_hEventStopThread );
  80. // erase old value
  81. m_hEventStopThread = NULL;
  82. }
  83. // return result of close operation
  84. return bResult;
  85. }
  86. UINT CALLBACK CThread::ThreadProc( LPVOID lpvParam )
  87. {
  88. // get pointer to this
  89. CThread *pThread = (CThread*) lpvParam;
  90. // call thread procedure
  91. UINT nResult = pThread->ThreadProc();
  92. // ensure to close thread normally
  93. // _endthreadex( (UINT)nResult );
  94. // self destroy if nessessary
  95. if( pThread->m_bSelfDestroy )
  96. {
  97. // close event handle
  98. if( pThread->m_hEventStopThread )
  99. ::CloseHandle( pThread->m_hEventStopThread );
  100. // close thread handle
  101. if( pThread->m_hThread )
  102. ::CloseHandle( pThread->m_hThread );
  103. // delete itself
  104. delete pThread;
  105. }
  106. //return with result
  107. return nResult;
  108. }