MonitorThread.cpp
上传用户:chenhai826
上传日期:2007-04-11
资源大小:72k
文件大小:3k
源码类别:

破解

开发平台:

Visual C++

  1. // MonitorThread.cpp: implementation of the CMonitorThread class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "NoPassword.h"
  6. #include "MonitorThread.h"
  7. #include "NPView.h"
  8. #include "NPDoc.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. extern HANDLE g_hStopAttack;
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18. CMonitorThread::CMonitorThread(CNPView *pView)
  19. :CThread(pView->GetParent())
  20. {
  21. CNPDoc *pDoc = pView->GetDocument();
  22. m_pnMaxThreads = &(pDoc->m_nMaxThreads);
  23. m_pnCurrentPwd = &(pDoc->m_nCurrent);
  24. m_nTo = pDoc->m_nTo;
  25. m_hWnd = pView->GetSafeHwnd();
  26. CAttackThread::m_Host = pDoc->m_Host;
  27. CAttackThread::m_User = pDoc->m_User;
  28. CAttackThread::m_hMainWnd = pView->GetSafeHwnd();
  29. CAttackThread::m_nNum = 0;
  30. }
  31. CMonitorThread::~CMonitorThread()
  32. {
  33. }
  34. //if want to continue, return TRUE
  35. BOOL CMonitorThread::Work()
  36. {
  37. if(DidSomeoneDie())
  38. {
  39. int count = *m_pnMaxThreads - m_ThreadList.GetCount();
  40. while(count-->0)
  41. {
  42. CAttackThread *p = new CAttackThread(NULL,*m_pnCurrentPwd);
  43. InterlockedIncrement(m_pnCurrentPwd);
  44. p->CreateThread();
  45. m_ThreadList.AddTail(p);
  46. InterlockedIncrement(&CAttackThread::m_nNum);
  47. }
  48. PostMessage(m_hWnd,UM_THREADNUM_UPDATE,CAttackThread::m_nNum,0);
  49. }
  50. else
  51. StopAllThread();
  52. return TRUE;
  53. }
  54. //true: some thread dead.
  55. //false: user stop.
  56. bool CMonitorThread::DidSomeoneDie()
  57. {
  58. DWORD index = 1;
  59. bool die = false;
  60. //have the user stop attacking?
  61. if(WaitForSingleObject(g_hStopAttack,0)!=WAIT_TIMEOUT)
  62. return false;
  63. if(m_ThreadList.GetCount() < *m_pnMaxThreads)
  64. return true;
  65. HANDLE *HandleArray = new HANDLE[m_ThreadList.GetCount() + 1];
  66. HandleArray[0] = g_hStopAttack;
  67. POSITION pos = m_ThreadList.GetHeadPosition();
  68. while(pos)
  69. HandleArray[index++] =  m_ThreadList.GetNext(pos)->m_hThread;
  70. index = WaitForMultipleObjects(index,HandleArray,FALSE,INFINITE);
  71. ASSERT(index != WAIT_FAILED);
  72. index -= WAIT_OBJECT_0;
  73. if(index == 0)
  74. die = false;
  75. else
  76. {
  77. die = true;
  78. pos = m_ThreadList.GetHeadPosition();
  79. while(pos)
  80. {
  81. POSITION temp_pos = pos;
  82. CAttackThread *p = m_ThreadList.GetNext(pos);
  83. HANDLE h = p->m_hThread;
  84. if(WaitForSingleObject(h,0) != WAIT_TIMEOUT) //it has dead
  85. { //KillThread also release the memory of the object
  86. p->KillThread();
  87. m_ThreadList.RemoveAt(temp_pos);
  88. }
  89. }
  90. }
  91. delete HandleArray;
  92. return die;
  93. }
  94. void CMonitorThread::CleanupWork(void)
  95. {
  96. StopAllThread();
  97. }
  98. void CMonitorThread::StopAllThread(void)
  99. {
  100. while(!m_ThreadList.IsEmpty())
  101. {
  102. CAttackThread *p = m_ThreadList.RemoveHead();
  103. p->KillThread();
  104. //KillThread() alse release the memory of the object.
  105. }
  106. }
  107. HANDLE g_hStopAttack = CreateEvent(NULL,TRUE,FALSE,NULL);