为什么我调用UpdateData(FALSE)时不立即更新我的对话框? [英] Why isn't my dialog box updated immediately when I call UpdateData(FALSE)?

查看:318
本文介绍了为什么我调用UpdateData(FALSE)时不立即更新我的对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我为什么调用 UpdateData(FALSE)不工作,直到我调用我的VC ++对话框应用程序中的消息框?运行时窗口保持不变,直到我调用:

  MessageBoxW(cDisp,L!!! Data Count !!!,MB_OK | MB_ICONINFORMATION)

我的代码片段的一部分就是这样...

  for(int j = 0; j <50; j ++)
{


if ferr new [15],m_dBiasC,WOld [1] [4],alpha,err,WNew [1] [4], y [15] [1],WTransOld [4] [1];

// do
// {
int iGraphX,iGraphY;
CString cDisp1(),cDisp;
m_dBiasC = m_dC;
err = m_dError;
alpha = m_dAlpha;
char ch;
double dummy1,dummy2;
cDisp.Format(L%d,j);
m_sCount.Format(L%d,m_iInterval);
UpdateData(FALSE);
BeginWaitCursor();
for(int i = 0; i <15; i ++)
{
f [0]>> dummy1>>>>
Time [i] = dummy1;
m_dAccX [i] = dummy2;
if(!f [0] .good())
{
MessageBox(L遇到第一个数据文件的结尾,LCaution
MessageBoxW(LQuiting Program,LCaution);
ferr = 2;
// break;
exit(1);
}
iGraphX = static_cast< int>(Time [i] * 100.0);
iGraphY = static_cast< int>(m_dAccX [i]);
m_Graph [0] .RemovePoint(0,0);
m_Graph [0] .AddPoint(iGraphX,iGraphY);
cDisp.Format(L%lf%lf,Time [i],m_dAccX [i]);
cDisp1 + = cDisp;
cDisp1 + =\r\\\
;
f [1]>> dummy1>>>>>> dummy2;
Time [i] = dummy1;
m_dAccY [i] = dummy2;
if(!f [1] .good())
{
MessageBox(L遇到第二个数据文件的结尾,LCaution
MessageBoxW(LQuiting Program,LCaution);
ferr = 3;
// break;
exit(1);
}
iGraphX = static_cast< int>(Time [i] * 50.0);
iGraphY = static_cast< int>(m_dAccY [i] * 10);
m_Graph [1] .RemovePoint(0,0);
m_Graph [1] .AddPoint(iGraphX,iGraphY);

f [2]>>> dummy1>>>>>> dummy2;
Time [i] = dummy1;
m_dAccZ [i] = dummy2;
if(!f [2] .good())
{
MessageBox(L遇到第三个数据文件的结尾,LCaution
MessageBoxW(LQuiting Program,LCaution);
ferr = 4;
// break;
exit(1);
}
iGraphX = static_cast< int>(Time [i] * 50.0);
iGraphY = static_cast< int>(m_dAccZ [i]);
m_Graph [2] .RemovePoint(0,0);
m_Graph [2] .AddPoint(iGraphX,iGraphY);
f [3]>>> dummy1>>>>>> dummy2;
Time [i] = dummy1;
m_dECG [i] = dummy2;
if(!f [3] .good())
{
MessageBox(L遇到第四个数据文件结束,LCaution
MessageBoxW(LQuiting Program,LCaution);
ferr = 5;
// break;
exit(1);
}
iGraphX = static_cast< int>(Time [i] * 100.0);
iGraphY = static_cast< int>(m_dECG [i] * 100);
m_Graph [3] .RemovePoint(0,0);
m_Graph [3] .AddPoint(iGraphX,iGraphY);
}
睡眠(500);
GetDlgItem(IDC_DISPLAYFILE) - > SetWindowTextW(cDisp1);
UpdateData(FALSE);
EndWaitCursor();
cDisp.Format(LData read until now:%d,((j + 1)* 15));
MessageBox(cDisp,L!!! Data Count !!!,MB_OK | MB_ICONINFORMATION);
UpdateData(FALSE);
//这部分用于自适应滤波器计算

for(int r = 0; r <15; r ++)
{
splusn [r] = static_cast< double>(m_dECG [r]);
X [r] [0] = m_dBiasC;
X [r] [1] = static_cast< double>(m_dAccX [r] * m_dScaleX);
X [r] [2] = static_cast< double>(m_dAccY [r] * m_dScaleY);
X [r] [3] = static_cast< double>(m_dAccZ [r] * m_dScaleZ);
}
WOld [0] [0] = m_dW0;
WOld [0] [1] = m_dW1;
WOld [0] [2] = m_dW2;
WOld [0] [3] = m_dW3;
for(int q = 0; q <4; q ++)
WNew [0] [q] = WOld [0] [q];
for(int p = 0; p <15; p ++)
{
for(int iRectCnt = 0; iRectCnt <60; iRectCnt ++)
{
f [4 ] for(int iWCnt = 0; iWCnt <4; iWCnt ++)
f [4] f [4]<< endl;
y [p] [0] = 0.0;
for(int q = 0; q <4; q ++)
WTransOld [q] [0] = WOld [0] [q]
for(int r = 0; r <4; r ++)
{
y [p] [0] = y [p] [0] WTransOld [r] [0]);
}
err = splusn [p] -y [p] [0];
for(int q = 0; q <4; q ++)
WNew [0] [q] = WOld [0] [q] +(2.0 * alpha * err * X [p] ]);
for(int q = 0; q <4; q ++)
WOld [0] [q] = WNew [0] [q];
}
cDisp.Format(L%lf,err);
GetDlgItem(IDC_ERROR) - > SetWindowTextW(cDisp);
splusn [p] = splusn [p] -err;
}
for(int p = 0; p <15; p ++)
{
iGraphX = static_cast< int>(Time [p] * 100.0);
iGraphY = static_cast< int>(splusn [p] * 100);
m_Graph [4] .RemovePoint(0,0);
m_Graph [4] .AddPoint(iGraphX,iGraphY);
}
cDisp.Format(L%lf,WNew [0] [0]);
GetDlgItem(IDC_W0C) - > SetWindowTextW(cDisp);
cDisp.Format(L%lf,WNew [0] [1]);
GetDlgItem(IDC_W1C) - > SetWindowTextW(cDisp);
cDisp.Format(L%lf,WNew [0] [2]);
GetDlgItem(IDC_W2C) - > SetWindowTextW(cDisp);
cDisp.Format(L%lf,WNew [0] [3]);
GetDlgItem(IDC_W3C) - > SetWindowTextW(cDisp);
m_sCount.Format(L%d,0);


//自适应过滤器结束
//如果已经遇到文件结束则关闭文件
if((f [0] .eof()| | f [1] .eof()|| f [2] .eof()|| f [3] .eof()))
{
MessageBox ;
ferr = 1;
exit(1);
}


//}while(!(f[0].eof())&!(f[1].eof())& &!(f [2] .eof())&&!(f [3] .eof()));
}

}


解决方案

有这么多的问题,我的代码,我甚至不知道从哪里开始。对于初学者:


  1. 为什么要调用 Sleep()?我不可能想象一个原因,你需要在一个精心设计的应用程序中这样做。 不要重复,因为您已在代码中显示。 Sleep 函数使您的线程暂停指定的毫秒数。尝试取出对 Sleep 的调用,看看是否不能解决您的问题。如果线程被挂起,UI如何更新?


  2. 为什么要将 UpdateData GetDlgItem 并调用 SetWindowText ?如果你让MFC负责使用对话框控件中显示的值来协调成员变量的值,则不需要自己设置它们的属性。


您也没有向我们展示足够的代码来了解 CDisp1 是,或 IDC_DISPLAYFILE 是。你最终想要完成什么??您要在处理数据时进行实时更新吗?




如果我们知道目标是什么,那么我们可以提供更好的帮助来构建代码。
更新:啊,我看到你刚刚添加了一个注释,上面的代码是在循环。所以你的问题实际上很简单: for 循环使CPU进入一个严格的循环,除了你的代码被执行。



调用 UpdateData 对于对话框中的控件的值没有任何结果,因为您的计算机的处理器当前正忙于循环。显示消息框通过创建自己的消息泵临时阻止对循环的执行,从而允许更新UI。取出 for 循环中的代码,并删除对 Sleep 的调用,您的问题就会消失。 / p>

从注释来看,看起来最好的事情是创建一个工作线程并放置你的循环,而不是从你的主(GUI)线程。然后,工作线程可以向您的主线程发送消息,以更新对话框中的控件。已提供完整的解决方案和更多信息,作为此问题的答案:有关Modeless对话框的问题在MFC


Can anyone tell me why calling UpdateData(FALSE) does not work until I call a messagebox in my VC++ dialog-based application? The runtime window remains unchanged until I call:

MessageBoxW(cDisp, L"!!!Data Count!!!", MB_OK | MB_ICONINFORMATION)  

A part of my code snippet is like this...

for(int j=0;j<50;j++)
        {


              if(ferr==0)
                {
                    double X[15][4],splusn[15],m_dBiasC,WOld[1][4],alpha,err,WNew[1][4],y[15][1],WTransOld[4][1];

                    //do
                    // {
                            int iGraphX,iGraphY;
                            CString cDisp1(""),cDisp;
                            m_dBiasC=m_dC;
                            err=m_dError;
                            alpha=m_dAlpha;
                            char ch;
                            double dummy1,dummy2;
                            cDisp.Format(L"%d",j);
                                m_sCount.Format(L"%d",m_iInterval);
                                UpdateData(FALSE);
                                BeginWaitCursor();
                            for(int i=0;i<15;i++)
                                {
                                    f[0]>>dummy1>>ch>>dummy2;
                                    Time[i]=dummy1;
                                    m_dAccX[i]=dummy2;
                                    if(!f[0].good())
                                        {
                                            MessageBox(L"The end of first data file encountered",L"Caution");
                                            MessageBoxW(L"Quiting Program ",L"Caution");
                                            ferr=2;
                                            //break;
                                            exit(1);
                                        }
                                    iGraphX=static_cast<int>(Time[i]*100.0);
                                    iGraphY=static_cast<int>(m_dAccX[i]);
                                    m_Graph[0].RemovePoint(0,0);
                                    m_Graph[0].AddPoint(iGraphX,iGraphY);
                                    cDisp.Format(L"%lf  %lf",Time[i],m_dAccX[i]);
                                    cDisp1+=cDisp;
                                    cDisp1+="\r\n";
                                    f[1]>>dummy1>>ch>>dummy2;
                                    Time[i]=dummy1;
                                    m_dAccY[i]=dummy2;
                                    if(!f[1].good())
                                        {
                                            MessageBox(L"The end of second data file encountered",L"Caution");
                                            MessageBoxW(L"Quiting Program ",L"Caution");
                                            ferr=3;
                                            //break;
                                            exit(1);
                                        }
                                    iGraphX=static_cast<int>(Time[i]*50.0);
                                    iGraphY=static_cast<int>(m_dAccY[i]*10);
                                    m_Graph[1].RemovePoint(0,0);
                                    m_Graph[1].AddPoint(iGraphX,iGraphY);

                                    f[2]>>dummy1>>ch>>dummy2;
                                    Time[i]=dummy1;
                                    m_dAccZ[i]=dummy2;
                                    if(!f[2].good())
                                        {
                                            MessageBox(L"The end of third data file encountered",L"Caution");
                                            MessageBoxW(L"Quiting Program ",L"Caution");
                                            ferr=4;
                                            //break;
                                            exit(1);
                                        }
                                    iGraphX=static_cast<int>(Time[i]*50.0);
                                    iGraphY=static_cast<int>(m_dAccZ[i]);
                                    m_Graph[2].RemovePoint(0,0);
                                    m_Graph[2].AddPoint(iGraphX,iGraphY);
                                    f[3]>>dummy1>>ch>>dummy2;
                                    Time[i]=dummy1;
                                    m_dECG[i]=dummy2;
                                    if(!f[3].good())
                                        {
                                            MessageBox(L"The end of fourth data file encountered",L"Caution");
                                            MessageBoxW(L"Quiting Program ",L"Caution");
                                            ferr=5;
                                            //break;
                                            exit(1);
                                        }
                                    iGraphX=static_cast<int>(Time[i]*100.0);
                                    iGraphY=static_cast<int>(m_dECG[i]*100);
                                    m_Graph[3].RemovePoint(0,0);
                                    m_Graph[3].AddPoint(iGraphX,iGraphY);
                                }
                            Sleep(500);
                            GetDlgItem(IDC_DISPLAYFILE)->SetWindowTextW(cDisp1);
                            UpdateData(FALSE);
                            EndWaitCursor();
                            cDisp.Format(L"Data Read till now: %d",((j+1)*15));
                            MessageBox(cDisp,L"!!!Data Count!!!",MB_OK|MB_ICONINFORMATION);
                            UpdateData(FALSE);
                            //This part is for adaptive filter calculations

                            for(int r=0;r<15;r++)
                                {
                                    splusn[r]=static_cast<double>(m_dECG[r]);
                                    X[r][0]=m_dBiasC;
                                    X[r][1]=static_cast<double>(m_dAccX[r]*m_dScaleX);
                                    X[r][2]=static_cast<double>(m_dAccY[r]*m_dScaleY);
                                    X[r][3]=static_cast<double>(m_dAccZ[r]*m_dScaleZ);      
                                }
                            WOld[0][0]=m_dW0;
                            WOld[0][1]=m_dW1;
                            WOld[0][2]=m_dW2;
                            WOld[0][3]=m_dW3;
                            for(int q=0;q<4;q++)
                                WNew[0][q]=WOld[0][q];
                            for(int p=0;p<15;p++)
                                {
                                    for(int iRectCnt=0;iRectCnt<60;iRectCnt++)
                                        {
                                            f[4]<<setiosflags(ios::fixed)<<setw(8)<<setprecision(4)<<p<<"       ";
                                            for(int iWCnt=0;iWCnt<4;iWCnt++) 
                                                f[4]<<setw(8)<<setprecision(2)<<WOld[0][iWCnt];
                                            f[4]<<endl;
                                            y[p][0]=0.0;
                                            for(int q=0;q<4;q++)
                                                WTransOld[q][0]=WOld[0][q];
                                            for(int r=0;r<4;r++)
                                            {
                                                y[p][0]=y[p][0]+(X[p][r]*WTransOld[r][0]);
                                            }
                                            err=splusn[p]-y[p][0];
                                            for(int q=0;q<4;q++)
                                                WNew[0][q]=WOld[0][q]+(2.0*alpha*err*X[p][q]);
                                            for(int q=0;q<4;q++)
                                                WOld[0][q]=WNew[0][q];
                                         }
                                    cDisp.Format(L"%lf",err);
                                    GetDlgItem(IDC_ERROR)->SetWindowTextW(cDisp);
                                    splusn[p]=splusn[p]-err;
                                 }
                            for(int p=0;p<15;p++)
                            {
                                iGraphX=static_cast<int>(Time[p]*100.0);
                                iGraphY=static_cast<int>(splusn[p]*100);
                                m_Graph[4].RemovePoint(0,0);
                                m_Graph[4].AddPoint(iGraphX,iGraphY);
                            }
                            cDisp.Format(L"%lf",WNew[0][0]);
                            GetDlgItem(IDC_W0C)->SetWindowTextW(cDisp);
                            cDisp.Format(L"%lf",WNew[0][1]);
                            GetDlgItem(IDC_W1C)->SetWindowTextW(cDisp);
                            cDisp.Format(L"%lf",WNew[0][2]);
                            GetDlgItem(IDC_W2C)->SetWindowTextW(cDisp);
                            cDisp.Format(L"%lf",WNew[0][3]);
                            GetDlgItem(IDC_W3C)->SetWindowTextW(cDisp);
                            m_sCount.Format(L"%d",0);


                            // End of Adaptive filter 
                            //close files here if already end of file is encountered
                            if((f[0].eof()||f[1].eof()||f[2].eof()||f[3].eof()))
                            {
                                MessageBox(L"End of File encountered");
                                ferr=1;
                                exit(1);
                            }


                     //}while(!(f[0].eof())&&!(f[1].eof())&&!(f[2].eof())&&!(f[3].eof()));
       }

    }

解决方案

There are so many problems with that code that I'm not even sure where to begin. For starters:

  1. Why are you calling Sleep()?? I can't possibly imagine a reason you would need to do this in a well-designed application. Especially not repeatedly, as you've shown in your code. The Sleep function causes your thread to be suspended for the specified number of milliseconds. Try taking out the calls to Sleep altogether, and see if that doesn't solve your problem. If the thread is suspended, how is the UI going to update?

  2. Why are you mixing UpdateData with GetDlgItem and a call to SetWindowText? If you're letting MFC take care of coordinating the values of your member variables with the values displayed in the dialog's controls, you shouldn't need to set their properties yourself.

You also don't show us enough of your code to know what CDisp1 is, or what IDC_DISPLAYFILE is. What are you ultimately trying to accomplish? Do you want a real-time update while data is processed? We could surely provide much better help on how to structure your code if we knew what the goal was, rather than just the code that doesn't work.


Update: Ahh, I see you've just added in a comment that the above code is in a for loop. So your problem is actually simple: a for loop puts the CPU into a tight loop where nothing else but your code gets executed. All message processing (in particular, updating the UI) is suspended while your loop executes the specified number of times.

Calling UpdateData isn't having any result on the values of the controls in the dialog because your computer's processor is currently busy looping. Showing a message box temporarily blocks the execution of your for loop by creating its own message pump, which allows the UI to be updated. Take the code out of the for loop, and remove the calls to Sleep, and your problems will go away.

From the comments, it looks like the best thing for you to do is create a worker thread and place your loop there instead, isolating it from your main (GUI) thread. The worker thread can then send a message to your main thread to update the controls in the dialog. Complete solutions and more information have already been provided as the answers to this question: Question about Modeless Dialog in MFC

这篇关于为什么我调用UpdateData(FALSE)时不立即更新我的对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆