locked
MFC_Break an infinite loop RRS feed

  • Question

  • Hi all, I got a question,

    I created an application for lucky draw. One dialog with two buttons, one is "start" the other one is "pause".
    Once user clicks start button, the random number will continous update.
    My question is how to break the infinite loop when I click "pause" button?

    For my case, when I click start button, and then my dialog is no response, however the number is updating.
    Need some hints.

    Thanks
    • Edited by Wild-Pointer Wednesday, February 3, 2010 12:57 AM
    Tuesday, February 2, 2010 1:47 PM

Answers

  • Perhaps you are using Sleep() in your Start handler? This si not a good method.

    If you start a timer in your Start handler and update the random number in the Timer handler, then you will be able to click the Pause button.

    David Wilkinson | Visual C++ MVP
    • Edited by davewilk Tuesday, February 2, 2010 2:03 PM (typo)
    • Proposed as answer by PaulH79 Tuesday, February 2, 2010 4:53 PM
    • Marked as answer by Wild-Pointer Wednesday, February 3, 2010 1:09 AM
    Tuesday, February 2, 2010 2:03 PM

All replies

  • Perhaps you are using Sleep() in your Start handler? This si not a good method.

    If you start a timer in your Start handler and update the random number in the Timer handler, then you will be able to click the Pause button.

    David Wilkinson | Visual C++ MVP
    • Edited by davewilk Tuesday, February 2, 2010 2:03 PM (typo)
    • Proposed as answer by PaulH79 Tuesday, February 2, 2010 4:53 PM
    • Marked as answer by Wild-Pointer Wednesday, February 3, 2010 1:09 AM
    Tuesday, February 2, 2010 2:03 PM
  • Having an infinite loop in the main UI thread is not a good idea.
    You can create a thread when the "start" button is clicked which then generates the random numbers.
    Now when the "pause" button is clicked you will be able to suspend the thread.

    The generated number in the thread can be sent to the main UI thread using a PostMessage.


    «_Superman_»
    Microsoft MVP (Visual C++)
    Tuesday, February 2, 2010 3:15 PM
  • Hi David,


    Thanks for your reply.
    I tried your method, and fix the simple problem.

    ------------
    Hi Superman,

    Thanks for your time.
    I will try later. Thanks.
    Tuesday, February 2, 2010 3:24 PM