I have 3 buttons on my form (Start / Resume, Clear, Exit). When you click on Start, N threads are created, and in the loop they continue to be executed (re-created) until the conditions are met. At this time, work with the form stops, i.e. it accepts push button messages, but does not process them until new threads finish their work.
Question: Is it possible to somehow give the user the opportunity to press buttons on the form with their subsequent processing? Sleep() , SuspendThread() , WaitForMultipleObjects(...,INFINITE) do not work because of the loop.
DWORD WINAPI ThreadFuncCycle(LPVOID lpParam) { HWND hDlg = (HWND)lpParam; SetWindowText(btnStart_Pause, "Resume"); for (int i = 0; i < 5; i++){ GetObject(cars[i].hBitmap, sizeof(BITMAP), &Bitmap); /* ΡΠΎΠ·Π΄Π°ΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ Ρ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠΎΠΌ ΠΎΠΊΠ½Π° ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ */ hCompatibleDC = CreateCompatibleDC(hdc); /* Π΄Π΅Π»Π°Π΅ΠΌ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ Π² ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ */ hOldBitmap = SelectObject(hCompatibleDC, cars[i].hBitmap); UpdateWindow(hDlg); StretchBlt(hdc, cars[i].x, cars[i].y, Bitmap.bmWidth / 2, Bitmap.bmHeight / 2, hCompatibleDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, SRCCOPY); /* Π²Π½ΠΎΠ²Ρ Π΄Π΅Π»Π°Π΅ΠΌ ΡΡΠ°ΡΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ */ SelectObject(hCompatibleDC, hOldBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ */ //DeleteObject(cars[i].hBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ */ DeleteDC(hCompatibleDC); } Sleep(500); DWORD index; BOOL in = false; DWORD buf[5] = { 0, 0, 0, 0, 0 }; BOOL finish[5] = { false, false, false, false, false }; BOOL pause[5] = { false, false, false, false, false }; do{ for (int i = 0; i < 5; i++){ buf[i] = cars[i].x; } for (int i = 0; i < 5; i++){ if (!finish[i]){ hThreads[i] = CreateThread(NULL, 0, ThreadFunc, &cars[i], CREATE_SUSPENDED, &dwThreadID[i]); SetThreadPriority(hThreads[i], THREAD_PRIORITY_IDLE); } } for (int i = 0; i < 5; i++){ if (!finish[i]){ ResumeThread(hThreads[i]); } } for (int i = 0; i < 5; i++){ /* ΠΏΠΎΠ»ΡΡΡΡ ΡΠ°Π·ΠΌΠ΅ΡΠ½ΠΎΡΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ */ if (!finish[i]){ GetObject(cars[i].hBitmap, sizeof(BITMAP), &Bitmap); /* ΡΠΎΠ·Π΄Π°ΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ Ρ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠΎΠΌ ΠΎΠΊΠ½Π° ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ */ hCompatibleDC = CreateCompatibleDC(hdc); /* Π΄Π΅Π»Π°Π΅ΠΌ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ Π² ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ */ hOldBitmap = SelectObject(hCompatibleDC, cars[i].hBitmap); RECT rect; rect.bottom = cars[i].y + Bitmap.bmHeight / 2; rect.left = buf[i] - Bitmap.bmWidth; rect.right = cars[i].x + Bitmap.bmWidth; rect.top = cars[i].y; InvalidateRect(hDlg, &rect, TRUE); UpdateWindow(hDlg); StretchBlt(hdc, cars[i].x, cars[i].y, Bitmap.bmWidth / 2, Bitmap.bmHeight / 2, hCompatibleDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, SRCCOPY); /* Π²Π½ΠΎΠ²Ρ Π΄Π΅Π»Π°Π΅ΠΌ ΡΡΠ°ΡΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ */ SelectObject(hCompatibleDC, hOldBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ */ //DeleteObject(cars[i].hBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ */ DeleteDC(hCompatibleDC); /* ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ ΠΎΡΠ½ΠΎΠ²Π½ΠΎΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ, Π·Π°Π²Π΅ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅ΡΠΈΡΠΎΠ²ΠΊΡ ΡΠ°Π±ΠΎΡΠ΅ΠΉ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΎΠΊΠ½Π° */ Sleep(100); } } bool check = true; for (int i = 0; i < 5; i++){ //sprintf(str, "i = %d,Lenght = %d",i, coordinate[i]); //MessageBox(NULL, str, "Info", MB_OK); if (cars[i].x >= rcClient.right - 300 && !finish[i]){ finish[i] = true; BITMAP bufBitmap; GetObject(cars[i].hBitmap, sizeof(BITMAP), &bufBitmap); GetObject(Numbers[whoFirst], sizeof(BITMAP), &Bitmap); /* ΡΠΎΠ·Π΄Π°ΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ Ρ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠΎΠΌ ΠΎΠΊΠ½Π° ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ */ hCompatibleDC = CreateCompatibleDC(hdc); /* Π΄Π΅Π»Π°Π΅ΠΌ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ Π² ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ */ hOldBitmap = SelectObject(hCompatibleDC, Numbers[whoFirst]); UpdateWindow(hDlg); StretchBlt(hdc, cars[i].x + bufBitmap.bmWidth / 2, cars[i].y, Bitmap.bmWidth / 12, Bitmap.bmHeight / 12, hCompatibleDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, SRCCOPY); /* Π²Π½ΠΎΠ²Ρ Π΄Π΅Π»Π°Π΅ΠΌ ΡΡΠ°ΡΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ ΡΠ΅ΠΊΡΡΠΈΠΌ */ SelectObject(hCompatibleDC, hOldBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ Π±ΠΈΡΠΌΠ°ΠΏ */ //DeleteObject(cars[i].hBitmap); /* ΡΠ΄Π°Π»ΠΈΡΡ ΡΠΎΠ²ΠΌΠ΅ΡΡΠΈΠΌΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ */ DeleteDC(hCompatibleDC); whoFirst += 1; } if (!finish[i]) check = false; } if (check){ in = true; } WaitForMultipleObjects(5, hThreads, TRUE, INFINITE); } while (!in); //WaitForMultipleObjects(5, hThreads, TRUE, 3000); for (int i = 0; i < 5; i++){ CloseHandle(hThreads[i]); finish[i] = false; whoFirst = 0; } return 0; }