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. 880228 - taking time in handling stops responding

880228 - taking time in handling stops responding

Scheduled Pinned Locked Moved C / C++ / MFC
questioncareer
6 Posts 3 Posters 1 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.
  • I Offline
    I Offline
    ilostmyid2
    wrote on last edited by
    #1

    hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx

    S N 2 Replies Last reply
    0
    • I ilostmyid2

      hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Windows needs to be given control, so it can route messages (like the 'button has been clicked' message) around your program. The usual solution is to do your long calculation in a worker thread[^].

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      • I ilostmyid2

        hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx

        N Offline
        N Offline
        norish
        wrote on last edited by
        #3

        If your calculation is just a calculation and never refers any other resources, I recommend the calculation is to be a thread and dialog button kickes start and stop only. I figure it very simply like belows.

        unsinged __stdcall calculation(void*)
        {
        // start real calculation
        }

        void CMyDialog::OnStart() {
        m_threadHandle = (HANDLE)_beginthreadex(NULL, 0, calculation, NULL, 0, &m_threadId);
        };

        void CMyDialog::OnCancel() {
        if (m_threadHandle) {
        TerminateThread(m_threadHandle, 0x13);
        CloseHandle(m_threadHandle);
        }
        };

        Consider the calculation() must be thread-safe and makes no resource leaks. If your calculation refers some other resources and should not be killed by TerminateThread(), another method is a stop flag refernce inside a loop. like;

        void calc(bool volatile f_stop) {
        for () {
        for () {
        for () {
        if (f_stop) goto exit_all_loop;
        }
        }
        }
        exit_all_loop:
        // clean up all resources
        }

        The function calc() is to be well designed at a point of cleaning up and performance trade-off.

        I 1 Reply Last reply
        0
        • N norish

          If your calculation is just a calculation and never refers any other resources, I recommend the calculation is to be a thread and dialog button kickes start and stop only. I figure it very simply like belows.

          unsinged __stdcall calculation(void*)
          {
          // start real calculation
          }

          void CMyDialog::OnStart() {
          m_threadHandle = (HANDLE)_beginthreadex(NULL, 0, calculation, NULL, 0, &m_threadId);
          };

          void CMyDialog::OnCancel() {
          if (m_threadHandle) {
          TerminateThread(m_threadHandle, 0x13);
          CloseHandle(m_threadHandle);
          }
          };

          Consider the calculation() must be thread-safe and makes no resource leaks. If your calculation refers some other resources and should not be killed by TerminateThread(), another method is a stop flag refernce inside a loop. like;

          void calc(bool volatile f_stop) {
          for () {
          for () {
          for () {
          if (f_stop) goto exit_all_loop;
          }
          }
          }
          exit_all_loop:
          // clean up all resources
          }

          The function calc() is to be well designed at a point of cleaning up and performance trade-off.

          I Offline
          I Offline
          ilostmyid2
          wrote on last edited by
          #4

          thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?

          N I 2 Replies Last reply
          0
          • I ilostmyid2

            thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?

            N Offline
            N Offline
            norish
            wrote on last edited by
            #5

            I think multi-threaded model is best practice for these case. Another approach is message processing like windows 3.1 ages programming style. Like this;

            bool f_stop = false; //global

            void CMyDialog::OnCancel() {
            ::f_stop = true;
            }

            void Yield() {
            MSG msg;
            for (int j = 0; j < 100; j++) {
            if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
            DispatchMessage(&msg);
            }
            }
            }

            void calc() {
            for () {
            for () {
            for() {
            // here is can quit calc
            Yield();
            if (f_stop) goto exit_all_loop;
            }
            }
            }
            exit_all_loop:
            // clean up resources
            }

            Above is rather old style but suitable some situations; (hate multi-threading or so.) But consider this code makes calc() so slower, so I think multi-threading is better.

            1 Reply Last reply
            0
            • I ilostmyid2

              thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?

              I Offline
              I Offline
              ilostmyid2
              wrote on last edited by
              #6

              i think it's better to comply with using a thread for this purpose. it's better not to alter the ordinary msg loop functionality. it does additional works like TranslateMessage, calling application's OnIdle, etc. which i don't intend to implement separately nor to lose what they do. thank u very much

              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