Method to allow abort of long processes

This is a simple way to abort from a long computation if the escape key is pressed. Just call escape_pressed() periodically from your procedure, and if TRUE is returned, abort.


BOOL escape_pressed() // return TRUE if escape pressed
{
 MSG msg;
 // check for messages and remove from queue
 if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) && msg.message == WM_KEYDOWN)
 {
  if (VK_ESCAPE == msg.wParam) // escape pressed
   return TRUE;
 }
 return FALSE;
}
One issue is that calling escape_pressed causes the wait cursor to go away. To resolve this, just call Restore as follows:

CWaitCursor hg; //  show wait cursor
...from inside a loop doing a lengthy computation

if (escape_pressed() )
{
 break;
}
else
{
 // restore wait cursor. escape_pressed() causes wait
 // cursor to go away
 hg.Restore(); 
}


Comments

  • Thank you!

    Posted by Legacy on 01/18/2004 12:00am

    Originally posted by: Andy Shi

    Great Tip!

    Reply
  • Additional comments

    Posted by Legacy on 12/23/1999 12:00am

    Originally posted by: Colin Elligsen

    Thanks for the the article and function.

    While playing with the example code I found out some interesting things.

    Below is an example of using the Peek function embedded in code rather than it's own function.

    The dialog box of which this is a part consists of a Start button (m_CtrlButtonStart) and an edit box (m_CtrlEditStart). The edit box counts up from 0 to 32000 when the user presses the Start button. m_CtrlStaticEsc is a static control which appears during the count up telling the user to press Esc to abort. When the start button is pressed it disappears, the static text appears, and the Ok and Cancel buttons are disabled.

    If the user presses the Esc during the count up, a dialog box is displayed to alert the user that they have stopped the count. The call to AfxMessageBox could easily ask the user if they really wanted to stop the count down or not, and the stop or continue appropriately.

    void CTryPeekDlg::OnButtonstart()
    {
    // control notification handler code
    char szMsg[10];
    CWaitCursor cw;

    m_CtrlButtonCancel.EnableWindow(FALSE);
    m_CtrlButtonOk.EnableWindow(FALSE);
    m_CtrlButtonStart.ShowWindow(SW_HIDE);

    // display "Press Esc to abort"
    m_CtrlStaticEsc.ShowWindow(SW_SHOW);

    for (int i=0; i<32001; i++)
    {
    cw.Restore();

    wsprintf(szMsg,"%d",i);
    m_CtrlEditStart.SetWindowText(szMsg);

    MSG msg;
    // check for messages and remove from queue
    if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) &&
    msg.message == WM_KEYDOWN)
    {
    if (VK_ESCAPE == msg.wParam) // escape pressed
    {
    AfxMessageBox("Cancelled");
    break;
    }
    }

    UpdateWindow();
    }

    m_CtrlButtonCancel.EnableWindow(TRUE);
    m_CtrlButtonOk.EnableWindow(TRUE);
    m_CtrlButtonStart.ShowWindow(SW_SHOW);
    m_CtrlStaticEsc.ShowWindow(SW_HIDE);
    }

    The first interesting thing is that you do need the call to cw.Restore even if you don't use the Peek in a seperate function (so you might as well use the function as originally written.)

    The second is that if I disable the Start button, the Esc key does not work. So instead of calling m_CtrlButtonStart.EnableWindow(FALSE) I call m_CtrlButtonStart.ShowWindow(SW_HIDE). This works by removing the button thus avoiding causing the user to think that they might be able to restart the count by hitting the start button during the count.

    Perhaps these things would be obvious to some, but I learned something from the original article and from messing around with it.

    Thanks again,
    Colin


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds