MonitorThread.cpp
上传用户:chenhai826
上传日期:2007-04-11
资源大小:72k
文件大小:3k
- // MonitorThread.cpp: implementation of the CMonitorThread class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "NoPassword.h"
- #include "MonitorThread.h"
- #include "NPView.h"
- #include "NPDoc.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern HANDLE g_hStopAttack;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMonitorThread::CMonitorThread(CNPView *pView)
- :CThread(pView->GetParent())
- {
- CNPDoc *pDoc = pView->GetDocument();
- m_pnMaxThreads = &(pDoc->m_nMaxThreads);
- m_pnCurrentPwd = &(pDoc->m_nCurrent);
- m_nTo = pDoc->m_nTo;
- m_hWnd = pView->GetSafeHwnd();
- CAttackThread::m_Host = pDoc->m_Host;
- CAttackThread::m_User = pDoc->m_User;
- CAttackThread::m_hMainWnd = pView->GetSafeHwnd();
- CAttackThread::m_nNum = 0;
- }
- CMonitorThread::~CMonitorThread()
- {
- }
- //if want to continue, return TRUE
- BOOL CMonitorThread::Work()
- {
- if(DidSomeoneDie())
- {
- int count = *m_pnMaxThreads - m_ThreadList.GetCount();
- while(count-->0)
- {
- CAttackThread *p = new CAttackThread(NULL,*m_pnCurrentPwd);
- InterlockedIncrement(m_pnCurrentPwd);
- p->CreateThread();
- m_ThreadList.AddTail(p);
- InterlockedIncrement(&CAttackThread::m_nNum);
- }
- PostMessage(m_hWnd,UM_THREADNUM_UPDATE,CAttackThread::m_nNum,0);
- }
- else
- StopAllThread();
- return TRUE;
- }
- //true: some thread dead.
- //false: user stop.
- bool CMonitorThread::DidSomeoneDie()
- {
- DWORD index = 1;
- bool die = false;
- //have the user stop attacking?
- if(WaitForSingleObject(g_hStopAttack,0)!=WAIT_TIMEOUT)
- return false;
- if(m_ThreadList.GetCount() < *m_pnMaxThreads)
- return true;
- HANDLE *HandleArray = new HANDLE[m_ThreadList.GetCount() + 1];
- HandleArray[0] = g_hStopAttack;
- POSITION pos = m_ThreadList.GetHeadPosition();
- while(pos)
- HandleArray[index++] = m_ThreadList.GetNext(pos)->m_hThread;
- index = WaitForMultipleObjects(index,HandleArray,FALSE,INFINITE);
- ASSERT(index != WAIT_FAILED);
- index -= WAIT_OBJECT_0;
- if(index == 0)
- die = false;
- else
- {
- die = true;
- pos = m_ThreadList.GetHeadPosition();
- while(pos)
- {
- POSITION temp_pos = pos;
- CAttackThread *p = m_ThreadList.GetNext(pos);
- HANDLE h = p->m_hThread;
- if(WaitForSingleObject(h,0) != WAIT_TIMEOUT) //it has dead
- { //KillThread also release the memory of the object
- p->KillThread();
- m_ThreadList.RemoveAt(temp_pos);
- }
- }
- }
-
- delete HandleArray;
- return die;
- }
- void CMonitorThread::CleanupWork(void)
- {
- StopAllThread();
- }
- void CMonitorThread::StopAllThread(void)
- {
- while(!m_ThreadList.IsEmpty())
- {
- CAttackThread *p = m_ThreadList.RemoveHead();
- p->KillThread();
- //KillThread() alse release the memory of the object.
- }
- }
- HANDLE g_hStopAttack = CreateEvent(NULL,TRUE,FALSE,NULL);