先看代码吧,多线程,一个进程可以有多个线程。
#include<windows.h>//要用到Sleep函数时要包含windows.h头文件 #include<iostream.h> DWORD WINAPI Fun1Proc(LPVOID lpParameter);//函数申明 DWORD WINAPI Fun2Proc(LPVOID lpParameter);//函数申明 int tickets = 100; /////////////////////////////////////////////////////////////////////////////////// void main() { HANDLE hThread1;//定义句柄 HANDLE hThread2; hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);//创建线程1,对应函数地址为Fun1Proc hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);//创建线程2,对应函数地址为Fun2Proc CloseHandle(hThread1);//关闭句柄,不再管 CloseHandle(hThread2); cout<<"the main thread"<<endl; while(1);//死等空出时间来让其他线程执行。 } DWORD WINAPI Fun1Proc(LPVOID lpParameter) { while(TRUE) { if(tickets>0) { cout<<"thread1 is running" << tickets-- << endl; } else { break; } } return 0; } DWORD WINAPI Fun2Proc(LPVOID lpParameter) { while(TRUE) { if(tickets>0) { cout<<"thread2 is running" << tickets-- << endl; } else { break; } } return 0; }
后面是WinMain函数
#include<windows.h> #include<stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd,//handle to window UINT uMsg,//message identifier WPARAM wParam,//first message parameter LPARAM lParam//second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASS wndcls; wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hbrBackground = (HBRUSH)GetStockObject(SYSTEM_FONT); wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(NULL, IDI_ERROR); wndcls.hInstance = hInstance; wndcls.lpfnWndProc = WinSunProc; wndcls.lpszClassName = NULL; wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); HWND hWnd; hWnd = CreateWindow("Weixin2003", "王涛", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL,hInstance, NULL); ShowWindow(hWnd, SW_SHOWNORMAL); UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch (uMsg) { case WM_CHAR: char szChar[20]; sprintf(szChar, "char is %d", wParam); MessageBox(hwnd, szChar, "weixin", 0); break; case WM_LBUTTONDOWN: MessageBox(hwnd, "mouse clicked", "weixin", 0); HDC hdc; hdc=GetDC(hwnd); TextOut(hdc, 0, 50, "惊涛骇浪", strlen("惊涛骇浪")); ReleaseDC(hwnd, hdc); break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hwnd, &ps); TextOut(hDC, 0, 0, "交大", strlen("交大")); EndPaint(hwnd, &ps); break; case WM_CLOSE: if(IDYES == MessageBox(hwnd, "是否真的结束", "weixin", MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }
搞了这么久的MFC还是一头雾水,不过还好,总之要对MFC整体框架要有一个总得认识。
#include<windows.h> #include<iostream.h> DWORD WINAPI Fun1Proc(LPVOID lpParameter); DWORD WINAPI Fun2Proc(LPVOID lpParameter); HANDLE hMutex; void main() { HANDLE hThread1; HANDLE hThread2; hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL); hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL); CloseHandle(hThread1); CloseHandle(hThread2); //线程创建函数 hMutex = CreateMutex(NULL, TRUE, "tickets");//创建一个互斥体 /*返回值类型Long,如执行成功,就返回互斥体对象的句柄;零表示出错。会设置GetLastError。即使返回的是一个有效句柄,但倘若指定的名字已经存在,GetLastError也会设为ERROR_ALREADY_EXISTS */ if(hMutex) { if(ERROR_ALREADY_EXISTS == GetLastError()) { cout << "only instance can run!"<<endl; return; } } WaitForSingleObject(hMutex, INFINITE); ReleaseMutex(hMutex); ReleaseMutex(hMutex); Sleep(4000); } DWORD WINAPI Fun1Proc(LPVOID lpParameter) { while(1) { WaitForSingleObject(hMutex, INFINITE); cout << "thread1 is running" << endl; //return 0; } } DWORD WINAPI Fun2Proc(LPVOID lpParameter) { while(1) { WaitForSingleObject(hMutex, INFINITE); cout<<"thread2 is running" << endl; //return 0; } }