线程函数

CBaseThread类

BaseThread.h

 1 // BaseThread.h: interface for the CBaseThread class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_ADTHREAD_H__B8BA8873_31BA_4CDE_9BC2_45AD6FCDA5BD__INCLUDED_)  //#if !defined  防止重复定义
 6 #define AFX_ADTHREAD_H__B8BA8873_31BA_4CDE_9BC2_45AD6FCDA5BD__INCLUDED_ 
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once  //只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次
10 #endif // _MSC_VER > 1000
11 
12 #include "BaseCallBack.h"
13 
14 class CBaseThread  
15 {
16 public:
17 
18     CBaseThread();
19     virtual ~CBaseThread();  // 在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生。
20     DWORD Start(DWORD dwCreationFlags = 0);
21     virtual DWORD Stop(WORD timeout, BOOL bForceKill = FALSE);
22     DWORD Suspend();
23     DWORD Resume();
24     BOOL SetPriority(int nPriority);
25     int GetPriority();
26 
27     void SetICallBack(CBaseCallBack* pCallBack)  //函数指针 指向函数的首地址
28     {
29         this->m_pCallBack = pCallBack;  //改变指向类的指针的指向
30     }
31     BOOL GetThreadRunState() const {return m_bRunThread;}
32 
33     DWORD GetStartTime(){return m_dwStartTime;}
34     void ResetStartTime(){m_dwStartTime = GetTickCount();}
35     void SetStatisticalTime(DWORD dwTimeLen){m_dwStatisticalTime = dwTimeLen;} 
36     DWORD GetStatisticalTime(){return m_dwStatisticalTime;}
37 
38 protected:        
39     virtual DWORD ThreadMethod() = 0;  //纯虚函数
40 
41 private:
42     static DWORD WINAPI ThreadFunc( LPVOID pParam);    
43     
44 public:
45     HANDLE m_hThread;      //线程句柄
46     DWORD m_dwTID;        //线程ID
47     DWORD m_dwExitCode;   //线程退出码
48 
49     DWORD m_dwStartTime;
50     DWORD m_dwStatisticalTime;
51 
52 protected:
53     LPTHREAD_START_ROUTINE m_pThreadFunction;    //工作线程指针
54     BOOL m_bRunThread;         //线程是否继续运行的标志
55     CBaseCallBack *m_pCallBack;          //指向类的指针
56 
57 };
58 
59 #endif // !defined(AFX_ADTHREAD_H__B8BA8873_31BA_4CDE_9BC2_45AD6FCDA5BD__INCLUDED_)

BaseThread.cpp

  1 // ADThread.cpp: implementation of the CBaseThread class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "BaseThread.h"
  7 
  8 #ifdef _DEBUG
  9 #undef THIS_FILE
 10 static char THIS_FILE[]=__FILE__;
 11 #define new DEBUG_NEW
 12 #endif
 13 
 14 //////////////////////////////////////////////////////////////////////
 15 // Construction/Destruction
 16 //////////////////////////////////////////////////////////////////////
 17 
 18 CBaseThread::CBaseThread()
 19 {
 20     m_pThreadFunction = CBaseThread::ThreadFunc;
 21     m_bRunThread = FALSE;
 22     m_hThread = NULL;
 23 }
 24 
 25 CBaseThread::~CBaseThread()
 26 {
 27     if (m_hThread != NULL)
 28     {
 29         Stop(1000, true); 
 30     }
 31 }
 32 
 33 DWORD CBaseThread::Start(DWORD dwCreationFlags)         //线程开始
 34 {
 35     m_bRunThread = true;
 36     m_hThread = CreateThread(NULL, 0, m_pThreadFunction, this, dwCreationFlags, &m_dwTID);  //创建线程
 37 
 38     m_dwExitCode = (DWORD)-1;
 39 
 40     ResetStartTime();
 41 
 42     return GetLastError();
 43 }
 44 /************************************************************************/
 45 /* 功能:停止线程
 46 /* 参数:bForceKill,若TRUE,强制立即结束,否则,温柔结束   
 47          timeout:为等待时间,一毫秒记                                                       
 48 /************************************************************************/
 49 DWORD CBaseThread::Stop(WORD timeout, BOOL bForceKill)    //线程停止
 50 {
 51     if (m_hThread != NULL)
 52     {
 53         //尝试"温柔地"结束线程 
 54         if (m_bRunThread)
 55         {
 56             m_bRunThread = FALSE;
 57             WaitForSingleObject(m_hThread, timeout); //WaitForSingleObject()在某一线程中调用该函数时,线程暂时挂起,如果在挂起的dwMilliseconds毫秒内,线程所等待的对象变为有信号状态,则该函数立即返回;如果超时时间已经到达dwMilliseconds毫秒,但hHandle所指向的对象还没有变成有信号状态,函数照样返回。
 58         }
 59         
 60         GetExitCodeThread(m_hThread, &m_dwExitCode);  //获取一个已中止线程的退出代码
 61         
 62         if (m_dwExitCode == STILL_ACTIVE && bForceKill)
 63         {    
 64             //强制杀死线程
 65             TerminateThread(m_hThread, DWORD(-1));  //在线程外终止一个线程,用于强制终止线程 DWORD dwExitCode 退出码
 66             m_hThread = NULL;
 67         }
 68     }
 69 
 70     return m_dwExitCode;
 71 }
 72 
 73 /************************************************************************/
 74 /*功能:挂起线程
 75 /************************************************************************/
 76 
 77 DWORD CBaseThread::Suspend()
 78 {
 79     return SuspendThread(m_hThread);
 80 }
 81 
 82 /************************************************************************/
 83 /*功能:恢复线程 
 84 /************************************************************************/
 85 DWORD CBaseThread::Resume()
 86 {
 87     return ResumeThread(m_hThread);
 88 }
 89 
 90 /************************************************************************/
 91 /*功能:设置线程优先级 
 92 /*参数:priority:查看SetThreadPriority() in windows sdk,其可能取值。
 93 /************************************************************************/
 94 BOOL CBaseThread::SetPriority(int nPriority)
 95 {
 96     return SetThreadPriority(m_hThread, nPriority);
 97 }
 98 
 99 /************************************************************************/
100 /* 功能:获得线程优先级
101 /************************************************************************/
102 int CBaseThread::GetPriority()
103 {
104     return GetThreadPriority(m_hThread);
105 }
106 
107 /************************************************************************/
108 /* 功能:静态线程函数, 调用实际的工作函数ThreadMethod()
109 /* 参数:pParam:在需要启用该线程函数的子类实例中,将this传给线程函数,以便线程函数可以访问该实例的成员。                                                                
110 /************************************************************************/
111 DWORD WINAPI CBaseThread::ThreadFunc(LPVOID pParam)
112 {
113     CBaseThread *pParent = reinterpret_cast<CBaseThread*>(pParam); //reinterpret_cast<type-id> (expression)强制类型转换符
114                                                                     //type-id 必须是一个指针、引用、算术类型、函数指针或者成员指针
115     pParent->ThreadMethod();    //多态性,调用子类的实际工作函数
116     return 0;
117 }

BaseCallBack.h

 1 // BaseCallBack.h: interface for the CBaseCallBack class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #ifndef _BASECALLBACK_H_
 6 #define _BASECALLBACK_H_
 7 
 8 typedef short I16;
 9 
10 class CBaseCallBack   
11 {
12 public:
13     virtual ~CBaseCallBack(){}
14     virtual void CallBackMethod(short *psiBuf, DWORD dwSize) = 0;
15     virtual void CallBackMethod() = 0;
16     virtual void CallBackMethod(WPARAM wPara){;}
17 };
18 
19 #endif

 

posted @ 2018-05-28 10:18  小雨滴答  阅读(778)  评论(0编辑  收藏  举报