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: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds