Multithreading wxwidgets:当工作线程完成时,如何杀死progressbar?

Multithreading wxwidgets:当工作线程完成时,如何杀死progressbar?,multithreading,progress-bar,wxwidgets,Multithreading,Progress Bar,Wxwidgets,用户界面有一个启动按钮,单击该按钮可启动工作线程,该线程执行以下操作: - starts local bluetooth radio. - start searching for remote radios. 在工作线程执行此操作时,我希望显示一个进度条。以下是我的计划: void MyFrame::OnStart(wxCommandEvent& event) { /* launch worker thread */ int count = 0; bool bSkip

用户界面有一个启动按钮,单击该按钮可启动工作线程,该线程执行以下操作:

- starts local bluetooth radio.
- start searching for remote radios.
在工作线程执行此操作时,我希望显示一个进度条。以下是我的计划:

void MyFrame::OnStart(wxCommandEvent& event)
{
   /* launch worker thread */
   int count = 0;
   bool bSkip = false;
   while (1)
   {
     wxProgressBar *p_pb = new wxProgressBar(...);
     count++;
     Sleep(500);
     /* update progress bar to new value */
     p_pb->update(count, wxStringEmpty, &bSkip);

     /* need a way to get out of here when the worker thread is done */
   }
}

/* custom event, fired when bluetooth is done */
void MyFrame::OnBluetoothSearchDone(wxCommandEvent& event)
{
   /* we know the thread is done because this event is fired */
   /* get data (if any) from the bluetooth module */
}
正如您所看到的,在我触发工作线程之后,主UI线程被卡在progressbar循环中,并且永远不会捕获工作线程发送的事件


如果有一种方法可以检查挂起的事件,那么我可以在progressbar循环中进行检查,如果有,那么我可以突破?或者从空闲函数调用progressbar上的update方法???

在wxWidgets中没有wxProgressBar这样的东西。如果您使用的是wxProgressDialog及其更新方法,则应该已经调度事件,因为它在内部调用WxField。如果你不这样做,你也可以自己做——但要确保你明白这样做的危险性或者,更好的做法是,完全避免进入循环,只需禁用无法使用的UI元素,并使用计时器更新进度指示器。

我在工作午餐休息时从内存中编写了问题中的代码;所以,是的,你是对的。我所做的只是启动一个计时器并定期更新进度。当线程返回时,我只是停止计时器。实际上工作得很好:你能提供一个链接,上面说wxprogressdialog使用wxyeild吗?所以基本上我可以覆盖YeildFor方法并在那里进行更新,但我应该注意工作线程发布的事件。你可以覆盖YieldFor,但这很少有必要。只需像往常一样处理事件即可。