Source code of Windows XP (NT5)
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.

94 lines
2.0 KiB

  1. #include "StdAfx.h"
  2. #include "Thread.h"
  3. //---------------------------------------------------------------------------
  4. // Thread Class
  5. //---------------------------------------------------------------------------
  6. //---------------------------------------------------------------------------
  7. // Constructor
  8. //---------------------------------------------------------------------------
  9. CThread::CThread() :
  10. m_dwThreadId(0),
  11. m_hStopEvent(CreateEvent(NULL, TRUE, FALSE, NULL))
  12. {
  13. }
  14. //---------------------------------------------------------------------------
  15. // Destructor
  16. //---------------------------------------------------------------------------
  17. CThread::~CThread()
  18. {
  19. }
  20. //---------------------------------------------------------------------------
  21. // Start Thread
  22. //---------------------------------------------------------------------------
  23. void CThread::StartThread()
  24. {
  25. // reset exit event
  26. ResetEvent(m_hStopEvent);
  27. // create thread
  28. m_hThread = CreateThread(NULL, 0, ThreadProc, this, 0, &m_dwThreadId);
  29. if (m_hThread == NULL)
  30. {
  31. // ThrowError(HRESULT_FROM_WIN32(GetLastError()), _T("Unable to create thread."));
  32. }
  33. }
  34. //---------------------------------------------------------------------------
  35. // Stop Thread
  36. //---------------------------------------------------------------------------
  37. void CThread::StopThread()
  38. {
  39. SetEvent(m_hStopEvent);
  40. if (m_hThread != NULL)
  41. {
  42. WaitForSingleObject(m_hThread, INFINITE);
  43. }
  44. }
  45. //---------------------------------------------------------------------------
  46. // Thread Procedure
  47. //---------------------------------------------------------------------------
  48. DWORD WINAPI CThread::ThreadProc(LPVOID pvParameter)
  49. {
  50. // initialize COM library for this thread
  51. // setting thread concurrency model to multi-threaded
  52. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  53. if (SUCCEEDED(hr))
  54. {
  55. CThread* pThis = reinterpret_cast<CThread*>(pvParameter);
  56. try
  57. {
  58. pThis->Run();
  59. }
  60. catch (...)
  61. {
  62. ;
  63. }
  64. CoUninitialize();
  65. }
  66. return 0;
  67. }