Click here to Skip to main content
12,255,210 members (34,307 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C MFC
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

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 20-Sep-12 21:04pm
dkaku170
Edited 20-Sep-12 21:21pm
Jochen Arndt127.2K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 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:

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;
    }
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100