6.线程基础

1.线程共享进程的句柄表,句柄表针对每一个进程。

2.创建新线程不要使用操作系统的CreateThread,使用_beginthreadex.

 1 /********************************************************************
 2     created:    2020/07/11   16:47
 3     filename:     H:\2020-2021\Windows核心编程\Windows核心编程\6.线程基础\6.线程基础.cpp
 4     file base:    6.线程基础
 5     author:        大海
 6     
 7     purpose:    Practice makes perfect.
 8 *********************************************************************/
 9 #include <windows.h>
10 #include <stdio.h>
11 #include "resource.h"
12 #include <process.h>
13 
14 HANDLE handle = NULL;
15 unsigned int _stdcall ThreadFun(void* pPM)
16 {
17     while (true)
18     {
19         OutputDebugString(TEXT("111\r\n"));
20         Sleep(1000);
21     }
22 
23     return 0;
24 }
25 int CALLBACK MainDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
26     switch (uMsg)
27     {
28         case WM_CLOSE:
29         {
30             PostQuitMessage(0);
31             return 1;
32         }
33         case WM_INITDIALOG:
34         {//创建线程
35             handle=    (HANDLE)_beginthreadex(NULL, 0, &ThreadFun, 0, 0, NULL);
36             return 0;
37         }
38         case WM_COMMAND: {
39             switch (LOWORD(wParam))
40             {
41             ///关闭线程
42             case IDDELETE:
43                 OutputDebugString(TEXT("线程已关闭\r\n"));
44                 TerminateThread(handle,4);
45                 break;
46             //挂起线程
47             case IDSuspend:
48                 OutputDebugString(TEXT("线程已暂停\r\n"));
49 
50                 SuspendThread(handle);
51                 break;
52             case IDCRESUME:
53                 OutputDebugString(TEXT("线程已恢复\r\n"));
54 
55                 ResumeThread(handle);
56                 break;
57             default:
58                 break;
59             }
60             break;
61         }
62 
63         
64     }
65 
66     return 0;
67 }
68 
69 
70 
71 int WINAPI WinMain(HINSTANCE hInstanceExe, HINSTANCE , LPSTR pszCmdline, int ) {
72 
73         //显示界面
74         DialogBoxW(hInstanceExe, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, MainDlgProc);
75 
76         
77 
78 
79         return 0;
80 }

 

很有意思的是,点击几次挂起,就必须点击几次恢复,否则恢复不了线程。

posted @ 2020-07-11 17:49  a1094426901  阅读(71)  评论(0)    收藏  举报