实时显示变量变化 [英] Real-time display of variable changes

查看:89
本文介绍了实时显示变量变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我使用了代码:

UINT postThread(LPVOID pParam)
{
    CGA_SpikeDlg*p = (CGA_SpikeDlg*)pParam;
    p->RunTimer();
    return 0;
}
void CGA_SpikeDlg::threadNew()
{
    AfxBeginThread(postThread, this);
}

void CGA_SpikeDlg::RunTimer()
{
    SetTimer(1, 6000, NULL);
}

void task1(M_args Parameter_, double Mtime, double tempVB, double TimeStep, double m_I, int FlagParameter[], M_args_Bound Parameter_Bound[], int MaxGeneration, float gL, float C, const int POPULATION_SIZE, float crossver, float mutations, stringstream &strResult)
{
    solveGPU_cpp(Parameter_, Mtime, tempVB, TimeStep, m_I, FlagParameter, Parameter_Bound, MaxGeneration, gL, C, POPULATION_SIZE, crossver, mutations, strResult);
    //cout << "task1 says: " << endl;
}
void CGA_SpikeDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO:  在此添加消息处理程序代码和/或调用默认值
    switch (nIDEvent)
    {
        case 1:   //定时器1处理函数,定时发送数据进行更新
        {   

            CString cstr((strResult.str()).c_str());
            //str.Format("%f", duration);
            m_result.SetWindowText(cstr);
            UpdateData(false);
            break;
        }
    }
    CDialogEx::OnTimer(nIDEvent);
}
void CGA_SpikeDlg::OnBnClickedButtonRun()
{
    // TODO:  在此添加控件通知处理程序代码
    UpdateData(true);
    threadNew();
    thread t1(task1, Parameter_, Mtime, tempVB, TimeStep, m_I, FlagParameter, Parameter_Bound, MaxGeneration, gL, C, POPULATION_SIZE, crossver, mutations, std::ref(strResult));
    t1.join();
}

我想在我的EDIT上显示strResult的值.我想看看跑步时它是如何变化的.但是当我跑步时,窗口没有显示任何响应,经过一段时间后,它显示了最终结果,这不是我想要的.

I want to show the value of strResult on my EDIT. I want to see how it change when i run.But when i run,The window displays no response,After a period of time, it shows the final result,That's not what I want.

推荐答案

该线程是局部变量,将其连接会导致主线程阻塞并等待帮助线程t1完成.

The thread is local variable and joining it cause the main thread to block and wait the helper thread t1 to finish.

使线程成为指针或shared_ptr,如果您的对话框类确保您至少在析构函数中join使其成为成员,则将其设为成员.

Make the thread a pointer or shared_ptr, make it member if your dialog class make sure you join it in the destructor at least.

赞:

mThread = std::make_shared<std::thread>(task1, Parameter_, Mtime, tempVB, TimeStep, m_I, FlagParameter, Parameter_Bound, MaxGeneration, gL, C, POPULATION_SIZE, crossver, mutations, std::ref(strResult));

而且...您不需要窗口计时器线程.

And ... you don't need a thread for the window timers.

更新: 使踏面成为对话框的成员:

UPDATE: Make the tread a member of the dialog:

std::stared_ptr<std::thread> mThread;

将所有参数打包在结构或类中:

Pack all parameters in a struct or class:

struct Args
{
   M_args Parameter_;
   double Mtime;
   double tempVB;
   double TimeStep;
   double m_I;
   std::vector<int> FlagParameter;
   std::vector<M_args_Bound> Parameter_Bound;
   int MaxGeneration;
   float gL;
   float C;
   int POPULATION_SIZE;
   float crossver;
   float mutations;
};

复制结构中的所有数据,并将其与对strResult的引用一起传递给线程:

Copy all data in the struct and pass it to the thread together with a reference to the strResult:

mThread = std::make_shared<std::thread>(Args, std::ref(strResult));

加入析构函数.确保您没有两次启动线程.

join in the destructor. Make sure you do no start the thread twice etc..

这篇关于实时显示变量变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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