Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have created an mfc that is used display info about the attached device.
I want my mfc dialog to wait for the device until it gets attached. For this I have created a function that receives attach callback from the device.
Now what I want is that the mfc should continuously call this attach function(displaying a waiting for device MSG)until a success(or 1) is returned by that.

What i thought is to create an infinite while loop like this

C++
while(1)
{int x;
 x=attach()
 if(x)
 break;
}


attach() returns 1 when device is successfully attached.

I put that in the init function of mfcdlg.cpp just before return true.

But this does not work.Can someone plz provide a solution to the problem...
Posted
Updated 20-Sep-12 21:21pm
v3
Comments
Richard MacCutchan 21-Sep-12 3:29am    
"does not work" is not a useful description of your problem. Please provide some useful detail and diagnostic information.
dkaku 21-Sep-12 3:40am    
By this I mean that the application gets hanged up...and no dialog or msg can be seen...

1 solution

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 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();
    // 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;
    }
}
 
Share this answer
 
v3
Comments
Espen Harlinn 21-Sep-12 3:41am    
A good and reasonable suggestion :-D
Jochen Arndt 21-Sep-12 3:41am    
Thank you Espen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900