Click here to Skip to main content
15,895,142 members

Response to: Making mfc call a function continuously until success is returned.

Revision 1
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:

C++
class CMyDialog : public CMyDialog
{
protected:
    bool m_bAttached;
    UINT m_nTimer;
};

CMyDialog::CMyDialog()
{
    m_bAttached = false;
    m_nTimer = 0;
}

BOOL CMyDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    // Choose realistic time out value here
    // e.g. something in the range of 100 to 500 ms
    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);
}

// Call this when dialog is closed (OnOK, OnCancel())
void CMyDialog::StopTimer()
{
    if (m_nTimer)
    {
        KillTimer(m_nTimer);
        m_nTimer = 0;
    }
}
Posted 20-Sep-12 21:39pm by Jochen Arndt.
Tags: , ,