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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds