Friday, December 15, 2006

Hello,

I'm sure you probably have faced a situation where you perform some heavy calculations inside AutoCAD with or without a dialog running. In these cases AutoCAD may look frozen and this may look strange to the user because it may think AutoCAD has really frozen and then the user may try to close it.

Even if you use a progress bar like AutoCAD status bar if you change the current window and get back to AutoCAD it may look frozen in black/white colors.

This occurs because the processor focus all its resources into your heavy loop and does not save enough time to both AutoCAD and your dialog process its own messages. These messages will affect several things like, for instance, the dialog graphics update.

The below function will let AutoCAD do this and you may call it from inside your loop (maybe a FOR or WHILE heavy statements). This way, on each interaction, this function will let AutoCAD process its messages.

void PumpAcadMessages()
{
// Get AutoCAD application
CWinApp* app = acedGetAcadWinApp();
// Get Main window
CWnd* wnd = app->GetMainWnd();
// dispatch incoming sent messages

MSG msg;
while (::PeekMessage (&msg, wnd->m_hWnd, 0, 0, PM_NOREMOVE))
{
// if can't pump, break
if (!app->PumpMessage())
{
::PostQuitMessage(0);
break;
}
}
LONG lIdle = 0;
// Now it's time to MFC do the work
while (app->OnIdle(lIdle++));
}

If you also would like to process your dialog messages you just need to change the first two lines and use your dialog's CWnd pointer instead of AutoCAD window's pointer.

Cheers!

4 comments :

Anonymous said...

If you just use NULL for the hWnd parameter in PeekMessage, it will retrieve messages for any window belonging to the current thread.

Fernando Malard said...

Thanks Chuck!

Anonymous said...

Is possible you can provide a .net version? I got the exactly same problem in my .net project.


Thank you and Merry Christmas

Anonymous said...

Wes,

I just posted this info to
href="http://through-the-interface.typepad.com/through_the_interface/2006/12/highlighting_a_.html">my
blog
, but just in case:

I’d suggest trying the following in .NET…

System.Windows.Forms.Application.DoEvents();

Regards,

Kean