Method to allow abort of long processes | CodeGuru

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) […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 15, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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();
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.