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

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • IT decision support impacts all aspects of technology management, from governance and strategy to budgets and resource planning. IT decision support effectiveness often falls prey to data-driven challenges that make it difficult to understand the data in context. These challenges: overwhelming data volumes, heterogeneous data types, and growing data complexity. This Forrester Consulting Paper reports the three key findings from their study conducted, on behalf of BDNA, to test the hypothesis that data …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds