CWaitCursor::CWaitCursor
To display a wait cursor, just declare a CWaitCursor object before the code that performs the lengthy operation.
CWaitCursor( );
Remarks
The constructor automatically causes the wait cursor to be displayed.
When the object goes out of scope (at the end of the block in which the CWaitCursor object is declared), its destructor sets the cursor to the previous cursor. In other words, the object performs the necessary clean-up automatically.
You can take advantage of the fact that the destructor is called at the end of the block (which might be before the end of the function) to make the wait cursor active in only part of your function. This technique is shown in the second example below.
Nota
Because of how their constructors and destructors work, CWaitCursor objects are always declared as local variables — they're never declared as global variables, nor are they allocated with new.
Example
// The following example illustrates the most common case
// of displaying the wait cursor during some lengthy
// processing.
void LengthyFunction()
{
// perhaps you display a dialog box before displaying a
// wait cursor
CWaitCursor wait; // display wait cursor
// do some lengthy processing
Sleep(1000);
} // destructor automatically removes the wait cursor
// This example shows using a CWaitCursor object inside a block
// so the wait cursor is displayed only while the program is
// performing a lengthy operation.
void ConditionalFunction()
{
if (SomeCondition)
{
CWaitCursor wait; // display wait cursor in this block only
// do some lengthy processing
Sleep(1000);
} // at this point, the destructor removes the wait cursor
else
{
// no wait cursor--only quick processing
}
}
Requirements
Header: afxwin.h