如何防止长进程挂起wxwidgets窗口

How to prevent a long process to hangs wxwidgets window

本文关键字:wxwidgets 窗口 挂起 长进程 何防止      更新时间:2023-10-16

我有一个wxwindows应用程序,在按钮的onclick事件中,我有一段很长的过程,例如,我有这样的东西:

for(int i=1;i<100;i++)
{
    sleep(1000);
    gaugeProgress->SetValue(i);
      *textOutput<<i;
}

运行此代码会停止UI响应。我添加

Refresh();
Update();

之后

 *textOutput<<i;

但它没有起作用。

有什么办法可以让我把这些事件说出来吗?

我正在使用VS 20102 在Windows上工作

在这些情况下,我使用wxYield(),如下所示:

for(int i = 1; i < 100; i++)
{
  // sleep() freezes the program making it unresponsible.
  // sleep(1000);
  gaugeProgress->SetValue(i);
  *textOutput << i;
  // wxYield stops this function execution 
  // to process all the rest of stocked events 
  // including the paint event and resumes immediately.
  wxYield();
}

这将停止当前进程,并允许应用程序像绘制事件一样处理消息堆栈。

但我认为正确的方法应该是使用线程。

您可以在wxwindows中添加wxTimer成员,在窗口构造函数中启动它,例如:

m_timer.Start(1000);

然后用一个函数捕获计时器事件,例如:

void mywindow::OnTimer(wxTimerEvent& event)
{
  Refresh();
  Update();
}

请确保将事件连接到wxTimer成员。