【发布时间】:2012-11-05 21:44:07
【问题描述】:
我有一个 GUI 和一个工作线程,我想将数据从工作线程发送到 GUI。我正在使用 QueueEvent 和 wxThreadEvents 来保持模型视图分离。我以某种方式遇到了 baadf00d 错误。
const int EvtID = 42;
MyFrame::MyFrame()
{
...
// this seems to work correctly,
// but I'm including it in case it is part of the problem
Bind(wxEVT_THREAD, (wxObjectEventFunction)&MyFrame::OutputData, this, EvtID);
...
}
MyFrame::OutputData(wxThreadEvent* event)
{
// should get data from MyThread,
// but outputs 0xBA, 0xAD, 0xF0, 0x0D in successive calls
output << event->GetInt();
}
MyThread::CreateOutputWithLocal()
{
wxThreadEvent event(wxEVT_THREAD, EvtID);
event.SetInt(getData());
//pFrame is a wxEvtHandler*
pFrame->QueueEvent(event.Clone());
}
MyThread::CreateOutputWithPointer()
{
wxThreadEvent* event = new wxThreadEvent(wxEVT_THREAD, EvtID);
event->SetInt(getData());
//pFrame is a wxEvtHandler*
pFrame->QueueEvent(event); // QueueEvent() takes control of the pointer and deletes it
}
使用wxThreadEvent 的SetPayload() 和GetPayload() 或其SetExtraLong() 和GetExtraLong() 似乎没有任何区别。我需要什么才能让它工作?
【问题讨论】:
-
这看起来不像真正的代码(缺少函数返回类型&c),所以问题可能出在您没有向我们展示的部分,因为似乎没有任何问题这里。好吧,您应该从
Bind()参数中删除演员表,因为它是不必要的并且可能有害,否则我什么也看不到。 -
谢谢。是的,问题出在代码的另一部分。这部分是准确传输数据的,但是获取数据的部分却搞砸了。
-
啊,
Bind()不需要特定类型的函数指针,不像Connect(),这是表单生成器使用的和我从中复制的。所以我的电话应该是Bind(wxEVT_THREAD, &MyFrame::OutputData, this, EvtID);
标签: c++ multithreading wxwidgets