Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. for loop pause resume

for loop pause resume

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncareer
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RaymondM
    wrote on last edited by
    #1

    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

    N J 2 Replies Last reply
    0
    • R RaymondM

      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

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      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.

      Programming Blog utf8-cpp

      R 1 Reply Last reply
      0
      • R RaymondM

        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

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        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;
        

        }

        R 1 Reply Last reply
        0
        • N Nemanja Trifunovic

          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.

          Programming Blog utf8-cpp

          R Offline
          R Offline
          RaymondM
          wrote on last edited by
          #4

          Thanks, but I have so little experience with threads, I will have to bone up on it. Raymond

          1 Reply Last reply
          0
          • J Joe Woodbury

            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;
            

            }

            R Offline
            R Offline
            RaymondM
            wrote on last edited by
            #5

            Thanks, but I don't see how that can be made to deal with a loop. Raymond

            J 1 Reply Last reply
            0
            • R RaymondM

              Thanks, but I don't see how that can be made to deal with a loop. Raymond

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              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.

              R 1 Reply Last reply
              0
              • J Joe Woodbury

                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.

                R Offline
                R Offline
                RaymondM
                wrote on last edited by
                #7

                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

                J 1 Reply Last reply
                0
                • R 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

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  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.)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups