Ark_Zhang

导航

 
 1 #pragma once
 2 #include <Windows.h>
 3 
 4 typedef enum THREAD_STATUS
 5 {
 6     THREAD_RUN      = 1,
 7     THREAD_PAUSE    = 2,
 8     THREAD_STOP     = 3,
 9 }THREAD_STATUS;
10 
11 class CThread
12 {
13 public:
14     CThread(void);
15     ~CThread(void);
16 
17     //创建线程
18     virtual long create(LPTHREAD_START_ROUTINE thread_proc, void *param);
19 
20     //暂停线程,0:暂停,1:继续
21     virtual long pause(long stat);
22 
23     //退出线程
24     virtual long close(void);
25 public:
26     HANDLE      hThread;
27     HANDLE      hEventStop;
28     HANDLE      hEventPause;
29     long        status;//线程状态
30 };

 

 

 1 #include "Thread.h"
 2 
 3 CThread::CThread(void)
 4 {
 5     hEventStop = CreateEvent(NULL, FALSE, FALSE, NULL);
 6     hEventPause = CreateEvent(NULL, FALSE, FALSE, NULL);
 7     hThread = NULL;
 8 }
 9 
10 CThread::~CThread(void)
11 {
12     if (hThread)
13     {
14         close();
15     }
16     ::CloseHandle(hEventStop);
17     ::CloseHandle(hEventPause);
18     hEventStop = NULL;
19     hEventPause = NULL;
20 }
21 
22 //创建线程
23 long CThread::create(LPTHREAD_START_ROUTINE thread_proc, void *param)
24 {
25     if (hThread)
26     {
27         return -1;
28     }
29 
30     ResetEvent(hEventStop);
31     SetEvent(hEventPause);
32     status = THREAD_RUN;
33     hThread = CreateThread(NULL, 0, thread_proc, param, 0, NULL);
34     if (!hThread)
35     {
36         status = THREAD_STOP;
37         return -1;
38     } 
39     return 0;
40 }
41 
42 //暂停线程,0:暂停,1:继续
43 long CThread::pause(long stat)
44 {
45     switch (stat)
46     {
47     case 0:
48         if (status != THREAD_STOP)
49         {
50             status = THREAD_PAUSE;
51             ResetEvent(hEventPause);
52         }
53         break;
54     case 1:
55         if (status = THREAD_PAUSE)
56         {
57             status = THREAD_RUN;
58             SetEvent(hEventPause);
59         }
60         break;
61     default:
62         return -1;
63     }
64     return 0;
65 }
66 
67 //退出线程
68 long CThread::close(void)
69 {
70     if (!hThread)
71     {
72         return -1;
73     }
74 
75     long ret = 0;
76     SetEvent(hEventPause);
77     SetEvent(hEventStop);
78     if (WaitForSingleObject(hThread, 500))
79     {
80         ret = -1;
81     }
82     CloseHandle(hThread);
83     hThread = NULL;
84     status = THREAD_STOP;
85     return ret;
86 }

使用示例

 1 #pragma once
 2 
 3 #include "Thread.h"
 4 
 5 class CMyThread : public CThread
 6 {
 7 public:
 8     CMyThread(void);
 9     ~CMyThread(void);
10     virtual long create(void);
11     virtual long close(void);
12 };
 1 #include "MyThread.h"
 2 
 3 //线程函数
 4 DWORD WINAPI myThread(LPVOID lp);
 5 
 6 CMyThread::CMyThread(void)
 7 {
 8 }
 9 
10 CMyThread::~CMyThread(void)
11 {
12 }
13 
14 //创建线程
15 long CMyThread::create(void)
16 {
17 
18     // TODO: Add your code here
19     //
20 
21     return CThread::create(myThread, this);
22 }
23 
24 //退出线程
25 long CMyThread::close(void)
26 {
27     long ret = CThread::close();
28 
29     // TODO: Add your code here
30     //
31 
32     return ret;
33 }
34 
35 //线程函数
36 DWORD WINAPI myThread(LPVOID lp)
37 {
38     CMyThread *pThis = (CMyThread *)lp;
39     
40     while(1)
41     {
42         //线程暂停判断
43         if (pThis->status == THREAD_PAUSE)
44         {
45             while(WaitForSingleObject(pThis->hEventPause, 50) != WAIT_OBJECT_0)
46             {
47                 continue;
48             }
49         }
50 
51         //线程关闭判断
52         if (WaitForSingleObject(pThis->hEventStop, 0) == WAIT_OBJECT_0)
53         {
54             break;
55         }
56 
57         // TODO: Add your code here
58         //
59 
60     }
61     return 0;
62 }

 

posted on 2013-05-23 13:57  Ark_Zhang  阅读(389)  评论(0编辑  收藏  举报