Using an infinite (or even time consuming) loop is a bad idea. It will block your application with probably high CPU load. The best solution would be posting a user defined message to the dialog from a thread that waits for the attach event.
A simpler method is using a timer inside your dialog that polls the attach state:
class CMyDialog : public CDialog
{
protected:
bool m_bAttached;
UINT m_nTimer;
void KillTimer();
};
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_WM_TIMER()
END_MESSAGE_MAP()
CMyDialog::CMyDialog()
{
m_bAttached = false;
m_nTimer = 0;
}
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_nTimer = SetTimer(1, TIME_OUT_VALUE, NULL);
return TRUE;
}
void CMyDialog::OnTimer(UINT nIDEvent)
{
if (m_nTimer && attach())
{
m_bAttached = true;
StopTimer();
}
CDialog::OnTimer(nIDEvent);
}
void CMyDialog::StopTimer()
{
if (m_nTimer)
{
KillTimer(m_nTimer);
m_nTimer = 0;
}
}