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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • One of the most foolproof ways for an online system to confirm, "Is it really you?" is by adding two-factor authentication. Modern two-factor solutions have evolved to support new, complex technology models that change how we use data, including cloud computing and bring-your-own-device (BYOD). However, not all two-factor authentication solutions are created equal. This guide walks through some of the key area of differentiation between two-factor authentication solutions and provides some concrete criteria …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds