Click here to Skip to main content
11,484,752 members (76,628 online)
Rate this: bad
good
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 22:04pm
dkaku170
Edited 20-Sep-12 22:21pm
v3
Comments
Richard MacCutchan at 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 at 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 at 21-Sep-12 3:41am
   
A good and reasonable suggestion :-D
Jochen Arndt at 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
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 185
2 OriginalGriff 163
3 Raje_ 155
4 Mehdi Gholam 130
0 Sergey Alexandrovich Kryukov 8,494
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,199
3 Maciej Los 2,681
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2015
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