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; } 
  • It is necessary to carry out a cycle too in a separate flow. - Alexander Petrov
  • Cycle, i.e. data in it? Or with a meter? - Nikita Alekseev
  • And if I have work with the graphic component of the form in this cycle? What to do - Nikita Alekseev
  • @NikitaAlekseev synchronize. For example sending messages. Well, if for displaying graphs, data of a cycle or streams is used, then block them in the main thread for the duration of use. - dr. FIN
  • one
    Send messages from other threads to emails: SendMessage / PostMessage . See also APC . In general, show a piece of your code, it will clarify a lot. - Alexander Petrov

0