使mfc连续调用函数,直到返回成功. [英] Making mfc call a function continuously until success is returned.
问题描述
大家好,
我创建了一个mfc,用于显示有关所连接设备的信息.
我希望我的mfc对话框在连接设备之前等待该设备.为此,我创建了一个从设备接收附加回调的函数.
现在,我想要的是 mfc应该连续调用此附加函数(显示正在等待MSG的设备),直到返回成功(或1)为止.
我以为是要创建一个无限的while循环,就像这样
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()返回1.
我在返回true之前将其放在mfcdlg.cpp的init函数中.
但这无法正常工作.有人可以为这个问题提供解决方案吗?
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...
推荐答案
使用无限(甚至是耗时)循环是一个坏主意.它将阻塞您的应用程序,可能会占用较高的CPU负载.最好的解决方案是从等待附加事件的线程中向对话框发布用户定义的消息.
一种更简单的方法是在对话框中使用计时器来轮询附加状态:
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;
}
}
这篇关于使mfc连续调用函数,直到返回成功.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!