C++ Windows窗体显示与显示对话框
我正在尝试为windows创建一个小应用程序。我遇到了一个问题,混合了一个后台线程来处理一些数据。这个后台引擎需要有人更新应用程序gui(windows窗体)并从中获取信息 这里是基本的应用程序C++ Windows窗体显示与显示对话框,c++,windows,winforms,modal-dialog,C++,Windows,Winforms,Modal Dialog,我正在尝试为windows创建一个小应用程序。我遇到了一个问题,混合了一个后台线程来处理一些数据。这个后台引擎需要有人更新应用程序gui(windows窗体)并从中获取信息 这里是基本的应用程序 int main() { Engine engine; Gui g; engine.run(); // creates a new thread for engine logic g.ShowDialog(); bool running = false; while(1) { // Upda
int main() {
Engine engine;
Gui g;
engine.run(); // creates a new thread for engine logic
g.ShowDialog();
bool running = false;
while(1)
{
// Update gui with information from the engine
g.update(engine.GetState());
// transition to running
if(g.isRunning() && !running)
{
engine.play();
running = true;
}
// transition to stopped
else if(!g.isRunning() && running)
{
engine.stop();
running = false;
}
}
}
我的主要问题来自这样一个事实:Gui类是被管理的。参见下面的类声明
public ref class Gui : public System::Windows::Forms::Form
我真的无法将这两种东西混合在一起,起初我只想将引擎放到Gui中,但这不起作用,因为它是非托管的
您会注意到,这里的问题是调用ShowDialog(),因为这会使对话框成为模态,之后不会执行任何代码。但是,如果我使用Show()。。。Gui根本不更新或处理任何输入
解决方案:
我在Gui类中创建了一个后台工作程序,因此引擎包含在Gui中,但在另一个线程上运行
void InitializeBackgoundWorker()
{
this->backgroundWorker1 = gcnew System::ComponentModel::BackgroundWorker;
backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &Gui::backgroundWorker1_DoWork );
backgroundWorker1->RunWorkerAsync( );
}
delegate void UpdateCallback(int hp, int maxhp);
void UpdateGui(int hp, int maxhp)
{
this->playerHealthBar->Value = ((float)(hp)/(float)(maxhp) * 100.0f);
};
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
aBotEngine engine;
while(true)
{
engine.runnable(NULL);
array<Object^>^args = gcnew array<Object^>(2);
args[0] = engine.getPlayerHp();
args[1] = engine.getPlayerMaxHp();
this->playerHealthBar->Invoke(gcnew UpdateCallback(this, &Gui::UpdateGui), args);
}
};
void InitializeBackgoundWorker()
{
此->backgroundWorker1=gcnew System::ComponentModel::BackgroundWorker;
backgroundWorker1->DoWork+=gcnew-doworkenventhandler(这个,&Gui::backgroundWorker1\u-DoWork);
backgroundWorker1->RunWorkerAsync();
}
委托void UpdateCallback(int-hp,int-maxhp);
void UpdateGui(int-hp,int-maxhp)
{
此->玩家健康栏->值=((浮点)(hp)/(浮点)(最大hp)*100.0f);
};
无效backgroundWorker1\u工作(对象^sender,工作目标^e)
{
阿博滕发动机;
while(true)
{
engine.runnable(空);
数组^args=gcnew数组(2);
args[0]=engine.getPlayerHp();
args[1]=engine.getPlayerMaxHp();
this->playerHealthBar->Invoke(gcnew UpdateCallback(this,&Gui::UpdateGui),args);
}
};
据我所知,这是让后台线程更新windows窗体的正确方法。我相信这不是唯一的办法
void InitializeBackgoundWorker()
{
this->backgroundWorker1 = gcnew System::ComponentModel::BackgroundWorker;
backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &Gui::backgroundWorker1_DoWork );
backgroundWorker1->RunWorkerAsync( );
}
delegate void UpdateCallback(int hp, int maxhp);
void UpdateGui(int hp, int maxhp)
{
this->playerHealthBar->Value = ((float)(hp)/(float)(maxhp) * 100.0f);
};
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
aBotEngine engine;
while(true)
{
engine.runnable(NULL);
array<Object^>^args = gcnew array<Object^>(2);
args[0] = engine.getPlayerHp();
args[1] = engine.getPlayerMaxHp();
this->playerHealthBar->Invoke(gcnew UpdateCallback(this, &Gui::UpdateGui), args);
}
};
void InitializeBackgoundWorker()
{
此->backgroundWorker1=gcnew System::ComponentModel::BackgroundWorker;
backgroundWorker1->DoWork+=gcnew-doworkenventhandler(这个,&Gui::backgroundWorker1\u-DoWork);
backgroundWorker1->RunWorkerAsync();
}
委托void UpdateCallback(int-hp,int-maxhp);
void UpdateGui(int-hp,int-maxhp)
{
此->玩家健康栏->值=((浮点)(hp)/(浮点)(最大hp)*100.0f);
};
无效backgroundWorker1\u工作(对象^sender,工作目标^e)
{
阿博滕发动机;
while(true)
{
engine.runnable(空);
数组^args=gcnew数组(2);
args[0]=engine.getPlayerHp();
args[1]=engine.getPlayerMaxHp();
this->playerHealthBar->Invoke(gcnew UpdateCallback(this,&Gui::UpdateGui),args);
}
};
如果有人想知道,我使用Gui类中的后台工作程序解决了这个问题。如果您自己找到解决方案,最好创建答案并接受它…谢谢Fabio。我就是这么做的。