Guest User

Untitled

a guest
May 21st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "../Threads/cthread.h"
  3. #include "../Generic/tools.h"
  4. #include "../Generic/xboxtools.h"
  5. #include "../Debug/Debug.h"
  6. //end project dependencies
  7.  
  8.  
  9. //*********************************************************
  10. //default constructor for class CThread
  11. //*********************************************************
  12. CThread::CThread ()
  13. {
  14. //GenSRand();
  15.  
  16. name = "Unnamed";
  17. this->hThread = NULL;
  18. this->hThreadId = 0;
  19. this->hMainThread = GetCurrentThread ();
  20. this->hMainThreadId = GetCurrentThreadId ();
  21. this->Timeout = 0; //milliseconds
  22. terminate = 0;
  23. this->Timeout = 500; //milliseconds
  24.  
  25. //hEvent = CreateEvent(NULL,FALSE,FALSE,"");
  26. }
  27.  
  28. //*********************************************************
  29. //destructor for class CObject
  30. //*********************************************************
  31. CThread::~CThread ()
  32. {
  33. //waiting for the thread to terminate
  34. Terminate();
  35.  
  36. // CloseHandle(hEvent);
  37. //DebugMsg("Thread Finnish : %s, 0x%X",name.c_str(),(int)hThreadId);
  38. }
  39.  
  40. //*********************************************************
  41. //working method
  42. //*********************************************************
  43. unsigned long CThread::Process (void* parameter)
  44. {
  45. printf("CThread::Process\n");
  46. //a mechanism for terminating thread should be implemented
  47. //not allowing the method to be run from the main thread
  48. if (::GetCurrentThreadId () == this->hMainThreadId)
  49. return 0;
  50. else {
  51.  
  52. return 0;
  53. }
  54.  
  55. }
  56.  
  57. //*********************************************************
  58. //creates the thread
  59. //*********************************************************
  60. bool CThread::CreateThread (DWORD hdwID)
  61. {
  62. if (!this->IsCreated ()) {
  63.  
  64. param* this_param = new param;
  65. this_param->pThread = this;
  66. this->hThread = ::CreateThread (NULL, 0, (unsigned long (__stdcall *)(void *))this->runProcess, (void *)(this_param), CREATE_SUSPENDED, &this->hThreadId);
  67. XSetThreadProcessor(this->hThread, hdwID);
  68. ::ResumeThread(this->hThread);
  69.  
  70. Sleep(0);
  71. return this->hThread ? true : false;
  72. }
  73. return false;
  74.  
  75. }
  76.  
  77. //*********************************************************
  78. //creates the thread
  79. //*********************************************************
  80. int CThread::runProcess (void* Param)
  81. {
  82. CThread* thread;
  83. thread = (CThread*)((param*)Param)->pThread;
  84. delete ((param*)Param);
  85. return thread->Process (0);
  86. }
  87.  
  88. void CThread::Lock()
  89. {
  90. lock.Lock();
  91. }
  92.  
  93. void CThread::Unlock()
  94. {
  95. lock.Unlock();
  96. }
  97.  
  98. void CThread::TriggerEvent()
  99. {
  100.  
  101.  
  102. // SetEvent(hEvent);
  103.  
  104.  
  105. }
  106. /*
  107. void CThread::WaitForEvent(DWORD wait)
  108. {
  109. WaitForSingleObject(hEvent,wait);
  110. }*/
  111.  
  112. void CThread::Terminate()
  113. {
  114. if (this->hThread) {
  115. terminate = 1;
  116. TriggerEvent();
  117.  
  118. if (::WaitForSingleObject (this->hThread, this->Timeout) == WAIT_TIMEOUT)
  119. {
  120. DebugMsg("CThread","CThread: Forcing termination of thread %s 0x%X",name.c_str(),(int)hThreadId);
  121. //TerminateThread (this->hThread, 99);
  122. }
  123.  
  124. CloseHandle (this->hThread);
  125.  
  126. this->hThread = 0;
  127. }
  128. }
  129.  
  130. void CThread::SetThreadName( char* threadName)
  131. {
  132. Sleep(10);
  133. THREADNAME_INFO info;
  134. info.dwType = 0x1000;
  135. info.szName = threadName;
  136. DebugMsg("CThread", "This thread is named [%s]", threadName);
  137. info.dwThreadID = (DWORD)-1;
  138. info.dwFlags = 0;
  139.  
  140. __try
  141. {
  142. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  143. }
  144. __except(EXCEPTION_EXECUTE_HANDLER)
  145. {
  146. }
  147. }
Add Comment
Please, Sign In to add comment