今天学习了Wiindows SDK 的线程.主要是http://msdn2.microsoft.com/en-us/library/ms682516.aspx 为参考资料.写了一个两个线程读取、写入临界区数据的小程序。
// abc1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "abc1.h"
#include <commctrl.h> //公共控件类。搞了多年的DELPHI,觉得这东西真是难用。
#include <strsafe.h> //字符串处理头文件,有时间好好学习一下。
#pragma comment(lib, "comctl32.lib")
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
//自定义全局变量
int ivalue = 0;
CRITICAL_SECTION aCritical_Scetion;
HANDLE EventHandles[2];
HWND MainWnd;
DWORD threadIds[2];
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void CreateOneThread(LPDWORD threadId,BOOL Add);//我声明的建立线程的方法
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_ABC1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ABC1));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ABC1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_3DSHADOW);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_ABC1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case 200:
WCHAR astring [255];
GetWindowText((HWND)lParam,astring,255);
MessageBox(hWnd,astring,TEXT("Information"),MB_OK);
break;
case 100:
CreateOneThread(&threadIds[0],TRUE);
break;
case 101:
CreateOneThread(&threadIds[1],FALSE);
break;
case 102:
WCHAR aaa[300];
GetDlgItemText(hWnd,102,aaa,300);
MessageBox(hWnd,aaa,TEXT("OK"),MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
/*WNDCLASSEX aClass;
aClass.cbSize = sizeof(WNDCLASSEX);
aClass.hbrBackground = (HBRUSH)(COLOR_MENU);
aClass.cbWndExtra = 0;
aClass.hCursor = LoadCursor(hInst,IDC_ARROW);
aClass.lpfnWndProc = WndProc;
aClass.lpszClassName = TEXT("MyClass");
//aClass.lpszMenuName = TEXT"";
aClass.style = CS_VREDRAW|CS_HREDRAW;
RegisterClassEx(&aClass);
CreateWindow(TEXT("MyClass"),TEXT("MyWindows"),WS_CHILD|WS_VISIBLE,0,0,100,200,hWnd,0,hInst,0);
RECT rcl;
INITCOMMONCONTROLSEX icex;
InitCommonControls();
// Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
// CreateWindow(TEXT("BUTTON"),TEXT("按钮(&A)"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,10,10,100,20,hWnd,(HMENU)200,hInst,0);
// CreateWindow(TEXT("COMBOBOX"),TEXT("按钮(&A)"),WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,10,30,100,20,hWnd,(HMENU)201,hInst,0);
// CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("EDIT"),TEXT("按钮(&A)"),WS_CHILD|WS_VISIBLE|WS_BORDER,10,50,100,20,hWnd,(HMENU)202,hInst,0);
// CreateWindow(TEXT("LISTBOX"),TEXT("按钮(&A)"),WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,10,70,100,80,hWnd,(HMENU)203,hInst,0);
GetClientRect (hWnd, &rcl);
CreateWindow(WC_IPADDRESS
,
TEXT(""),
WS_CHILD|WS_VISIBLE ,
0,
0,
rcl.right - rcl.left,
rcl.bottom - rcl.top,
hWnd,
(HMENU) 205,
hInst,
NULL);
HANDLE mutexHandle ;
mutexHandle = CreateMutex(NULL,FALSE,TEXT("AAA"));
if (mutexHandle !=NULL&&GetLastError() == ERROR_ALREADY_EXISTS)
MessageBox(hWnd,TEXT("已有一个存在!"),TEXT("Title"),MB_OK);
InitializeCriticalSection();
EnterCriticalSection();
LeaveCriticalSection();
DeleteCriticalSection();
CreateSemaphore(NULL,10,100,TEXT("DDD"));
CloseHandle(mutesHandle);
OpenMutex
*/
CreateWindow(TEXT("BUTTON"),TEXT("加法"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,10,10,100,30,hWnd,(HMENU)100,((LPCREATESTRUCT)lParam)->hInstance,NULL);
CreateWindow(TEXT("BUTTON"),TEXT("减法"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,150,10,100,30,hWnd,(HMENU)101,((LPCREATESTRUCT)lParam)->hInstance,NULL);
//CreateWindow(TEXT("BUTTON"),TEXT("终止"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,150,10,100,30,hWnd,(HMENU)101,((LPCREATESTRUCT)lParam)->hInstance,NULL);
CreateWindow(TEXT("STATIC"),TEXT("减法"),WS_CHILD|WS_VISIBLE,10,50,100,30,hWnd,(HMENU)102,((LPCREATESTRUCT)lParam)->hInstance,NULL);
InitializeCriticalSection(&aCritical_Scetion);
MainWnd = hWnd;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
DWORD WINAPI OneThread(LPVOID lpThreadParameter)
{
while (true)
{
EnterCriticalSection(&aCritical_Scetion);
if ( *(LPBOOL)lpThreadParameter )
ivalue ++;
else
ivalue --;
TCHAR astr[MAX_LOADSTRING] ;
StringCchPrintf(astr,MAX_LOADSTRING,TEXT("第%d次运行"),ivalue);
SetDlgItemText(MainWnd,102,astr);
LeaveCriticalSection(&aCritical_Scetion);
Sleep(300);
}
delete lpThreadParameter;
return 0;
}
void CreateOneThread(LPDWORD threadId,BOOL Add)
{
BOOL *aAdd = new BOOL(Add);
CreateThread(NULL,0,&OneThread,aAdd,0,threadId);
}
主要学习的Mutes,Event,Semaphore,Thread,CriticalSection,WaitForSingleObject ,MultipleObjects 几个API函数。很好玩。