for loop pause resume
-
I have in a C++ module a for loop that can take some minutes to complete. I would like to add a pause/resume feature, and would also like to be able to stop it. However while the loop is running the program is totally unresponsive. Is there anything to be done ? Raymond Mercier
-
I have in a C++ module a for loop that can take some minutes to complete. I would like to add a pause/resume feature, and would also like to be able to stop it. However while the loop is running the program is totally unresponsive. Is there anything to be done ? Raymond Mercier
Move the loop into a background thread and on each iteration (or every nth one) check for a "pause" flag that can be set by the UI thread; just be sure it is done in a thread-safe manner.
-
I have in a C++ module a for loop that can take some minutes to complete. I would like to add a pause/resume feature, and would also like to be able to stop it. However while the loop is running the program is totally unresponsive. Is there anything to be done ? Raymond Mercier
For dialog boxes, you can add and periodically call the following. Adjust to get best responsiveness:
BOOL CMyDialog::Pump()
{
MSG msg;
while (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!AfxGetApp()->PumpMessage())
{
::PostQuitMessage(0);
return FALSE;
}
}// // Simulate the framework's idle processing mechanism. // LONG idle = 0; while (AfxGetApp()->OnIdle(idle++)); return TRUE;
}
-
Move the loop into a background thread and on each iteration (or every nth one) check for a "pause" flag that can be set by the UI thread; just be sure it is done in a thread-safe manner.
-
For dialog boxes, you can add and periodically call the following. Adjust to get best responsiveness:
BOOL CMyDialog::Pump()
{
MSG msg;
while (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!AfxGetApp()->PumpMessage())
{
::PostQuitMessage(0);
return FALSE;
}
}// // Simulate the framework's idle processing mechanism. // LONG idle = 0; while (AfxGetApp()->OnIdle(idle++)); return TRUE;
}
-
In the loop you periodically call the function; this keeps the message pump going and allows messages to be processed. This is the traditional way to make a progress/cancelable dialog box. The alternative, and preferable for anything lengthy, is to run the operation in a thread.
-
In the loop you periodically call the function; this keeps the message pump going and allows messages to be processed. This is the traditional way to make a progress/cancelable dialog box. The alternative, and preferable for anything lengthy, is to run the operation in a thread.
In the end it was easy enough to run the function with its loop in a thread. "ThreadProcTCP" is a rather involved module, with a loop, but it all works fine.
if (hChild)
GetExitCodeThread( hChild, &dwExitCodeThread );
if (dwExitCodeThread!=STILL_ACTIVE)
hChild = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProcTCP, hWnd, 0, &dwChild );RaymondM
-
In the end it was easy enough to run the function with its loop in a thread. "ThreadProcTCP" is a rather involved module, with a loop, but it all works fine.
if (hChild)
GetExitCodeThread( hChild, &dwExitCodeThread );
if (dwExitCodeThread!=STILL_ACTIVE)
hChild = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProcTCP, hWnd, 0, &dwChild );RaymondM
Note two things; a thread handle is non-signaled when it's running and signaled when it's not. Thus you can do a WaitForSingleObject() on a thread handle. Second: if you do any CRT calls in your thread, you should use __beginthreadex() (not all CRT calls need thread local storage (TLS), but figuring out which ones need it is tedious and, to my knowledge, not documented.)