how to break out infinite loop?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: how to break out infinite loop?

  1. #1
    Join Date
    Aug 1999
    Posts
    74

    how to break out infinite loop?

    I want to know in the infinite loop, how to keep track if user hit any key especially ESC key then break out of infinite loop.

    Here is my code:

    for (;
    {
    // please help me to figure it out this section here how to keep track if user hit any key
    // waiting user to hit any key
    // if user hit ESC key then break;
    // otherwise
    .... my tasks.....
    ..................
    }


  2. #2
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: how to break out infinite loop?

    1. Set a member variable to false before entering your loop.
    2. When you enter the loop, set a flag variable to show you're in the loop, and upon exiting the loop, reset the flag variable.
    3. Use ClassWizard to set up a PreTranslateMessage() handler.
    3. In the PreTranslateMessage(0 handler, look for key down. If the key is escape, and you're in your loop, set the member variable mentioned in step 1 to true.


    Sample code:


    // in your class header file
    bool m_bBreak; // will be true when loop needs to stop
    bool m_bInLoop; // only true when inside our loop

    // PreTranslateMessage() handler in your implementation file:
    BOOL CMyClass::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE && m_bInLoop)
    m_bBreak = true;

    return CDialog::PreTranslateMessage(pMsg);
    }

    // your loop:
    m_bBreak = false;
    m_bInLoop = true;
    while(!m_bBreak)
    {
    // Do your loop processing here.
    // When user hits the escape key, the handler will set m_bBreak to true,
    // causing your loop to exit
    }
    m_bInLoop = false;





  3. #3

    Re: how to break out infinite loop?

    Well you have written it already...


    for(;{
    if( need to break )
    break;
    }




    the break; will break out of the loop you are in, if you are in nested loops, it will break out of the inner and continue in the outer loop. A not very nice one but effective with many nested loops is to set a label and use goto.



    Regards Albert Bertilsson
    http://www.albert.nu

  4. #4
    Join Date
    Aug 1999
    Posts
    74

    To Bob:

    Hi Bob,
    I try it your way, but still not working. When Spy_Terminal function trigger, I try to hit ESC key doesn't do anything. Look like the while loop still running.

    Please show me where am I making mistake. Thanx in advance for helping me.

    void CAdSetupView::Spy_Terminal()
    {
    unsigned short port_error;
    CString strComMsg;
    CString strSpyMsg;

    MyComLink sio ( ComPortDlg.iComPort );
    GFSerial cp ( &sio );

    // Check for invalid com port
    if (cp.GetCommError(port_error) != GCPP_OK)
    {
    strComMsg.Format ("%d", ComPortDlg.iComPort+1);
    MessageBox ("Com " + strComMsg + " is not valid. Please pick a different Com Port.", "Invalid Com Port", MB_OK | MB_ICONSTOP);

    if (IDOK)
    return;
    }
    else
    {
    // flush characters from the receive buffer so we get new msg
    cp.RXFlush();
    bBreak = false;
    bInLoop = true;
    while (!bBreak)
    {
    cp.GetString (rcv_Spy, 500, '0x03', 500);
    m_strSpyMode.Format ("%s", rcv_Spy);
    UpdateWindow();
    UpdateData(false);
    }
    bInLoop = false;
    m_strSpyMode = "Congratulation! You got it.";
    UpdateData(false);
    }
    }

    BOOL CAdSetupView::PreTranslateMessage(MSG* pMsg)
    {
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE && bInLoop)
    bBreak = true;

    return CFormView::PreTranslateMessage(pMsg);
    }



  5. #5
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: To Bob:

    Where is bBreak declared?

    Set a breakpoint in PreTranslateMessage() at the "if(p->Msg == WM_KEYDOWN.." line (not on the "bBreak = true;" line), then run your program and hit the ESC key. Does the breakpoint get hit?


  6. #6
    Join Date
    Aug 1999
    Posts
    74

    Re: To Bob:

    bBreak declared in CAdSetupView class as public.
    I did set the break point on if (p->Msg....)
    it did hit it when the infinite function not trigger. Once the infinite loop trigger it never hit the break point.


  7. #7
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: how to break out infinite loop?

    I think I've answered this type of question half-a-dozen times. Here's a link to one of them:

    http://www.codeguru.com/bbs/wt/showp...age=0&Limit=25

    Regards,
    Alvaro


  8. #8
    Guest

    Re: how to break out infinite loop?

    If the while loop processing is done as a result of some input message, like a mouse click or key stroke then the call to PreTranslateMessage likely won't happen until the looping returns. I think you need to look closer at multi threading this.

    HTH.



  9. #9
    Join Date
    Aug 1999
    Posts
    74

    To Anonymous:

    In this case, I just want infinite loop running until the user hit ESC key then break out of infinite loop. Do you have any idea how to do this? Please let me know. Thanx in advance and I really appreciated for all the ideas and opinions.


  10. #10
    Join Date
    May 1999
    Location
    Belgium
    Posts
    37

    Re: how to break out infinite loop?

    An easy way to do this is


    for (;
    {
    //do your stuff
    if (GetAsyncKeyState(VK_ESCAPE)<0) break;
    }




    So you don't have to use the pretranslates/messagepumps/threads

    Hope this helps,

    Steven



  11. #11

    Re: how to break out infinite loop?

    #include <conio.h>

    for(...)
    {
    if(kbhit())
    {
    DestroyWindow();
    }
    }

    or just put it into the OnKeyDown function

    for(...)
    {
    if(nChar == VK_ESCAPE)
    {
    DestroyWindow();
    }
    }



  12. #12
    Guest

    To Steve:

    I want to detect if user hit pause key then pause the screen. I don't know what value gonna return if I check VK_PAUSE. Thanx Steve.

    ESC key return less than 0.
    Pause key return What value?????????????
    character return What value?????????????


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center