#include #include #include #include "resource.h" #define NMAXTHREADS 8 char szAppName[] = "w32-multithread"; char szAppClass[] = "w32app"; char szTitle[] = "Exemplu Multithread Win32"; int nClientWidth = 400; // Defineste zona int nClientHeight = 300; // de desenat int idx; // Index in tabloul de threaduri int i; // variabila de lucru HBRUSH hEraseBrush; HPEN hErasePen; HINSTANCE hInst; HWND hMainWnd; HDC hdc; RECT wndr; long culori[NMAXTHREADS] = { //0x00BBGGRR 0x000000FF, // Rosu 0x00008080, // Portocaliu 0x00FFFF00, // Galben 0x0000FF00, // Verde 0x00FF0000, // Albastru 0x00808080, // Gri 0x00FF00FF, // Mov 0x00FF8000}; // Azuriu typedef struct _ti { HANDLE hThread; DWORD tid; HANDLE hDoneEvent; int colorIdx; } TThreadInfo; TThreadInfo ThreadsTable[NMAXTHREADS]; // Functia de desenare in fereastra void Deseneaza(int x, int y, HPEN hPen) { SelectObject(hdc, hPen); MoveToEx(hdc, x,y, NULL); LineTo(hdc, x+1, y+1); } // Functia de definire a activitatii unui thread DWORD FunctieThread(void *) { int index = idx; // salvat local int xc=200, yc=150, // punct de pornire incX=index+1, incY=1; // incremente pe cele doua directii HPEN hPen = CreatePen(PS_SOLID, 5, culori[ThreadsTable[index].colorIdx]); while(1) { if(WaitForSingleObject( ThreadsTable[index].hDoneEvent, 2)==WAIT_OBJECT_0) break; // Deseneaza si oglindeste la margini Deseneaza(xc, yc, hPen); xc+=incX; if((xc < 0) || (xc > nClientWidth)) incX=-incX; yc+=incY; if((yc < 0) || (yc > nClientHeight)) incY=-incY; } DeleteObject(hPen); ExitThread(0); return 0; } // Functia fereastra LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lResult = TRUE; PAINTSTRUCT ps; HDC hc; switch(uMsg) { case WM_CREATE: hdc = GetDC (hWnd); hErasePen = CreatePen(PS_SOLID,1, 0x00000000); break; case WM_SIZE: GetClientRect(hWnd, &wndr); nClientWidth = wndr.right; nClientHeight= wndr.bottom; case WM_PAINT: hc = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; case WM_CLOSE: DestroyWindow(hWnd); // Genereaza WM_DESTROY break; case WM_DESTROY: DeleteObject(hErasePen); ReleaseDC(hWnd, hdc); for(i=0; i