4
A
回答
2
如果你只需要有一些工作在後臺,直到它完成 - 火,如果你會忘記,這樣的事情:
// warning: off the top of my head ;-)
class MyThread
: public wxThread
{
public:
MyThread() : wxThread(wxTHREAD_DETACHED)
{
if(wxTHREAD_NO_ERROR == Create()) {
Run();
}
}
protected:
virtual ExitCode Entry()
{
// do something here that takes a long time
// it's a good idea to periodically check TestDestroy()
while(!TestDestroy() && MoreWorkToDo()) {
DoSaidWork();
}
return static_cast<ExitCode>(NULL);
}
};
MyThread* thd = new MyThread(); // auto runs & deletes itself when finished
6
我使用過在wxWidgets中以幾乎所有的方式描述here,我可以說使用自定義事件雖然最初有點複雜,但從長遠來看可以節省一些頭痛的問題。 (該wxMessageQueue類是相當不錯的,但是當我使用它,我發現它泄漏;我沒有在一年內檢查它雖然。)
一個基本的例子:
MyFrm.cpp
#include "MyThread.h"
BEGIN_EVENT_TABLE(MyFrm,wxFrame)
EVT_COMMAND(wxID_ANY, wxEVT_MYTHREAD, MyFrm::OnMyThread)
END_EVENT_TABLE()
void MyFrm::PerformCalculation(int someParameter){
//create the thread
MyThread *thread = new Mythread(this, someParameter);
thread->Create();
thread->Run();
//Don't worry about deleting the thread, there are two types of wxThreads
//and this kind deletes itself when it's finished.
}
void MyFrm::OnMyThread(wxCommandEvent& event)
{
unsigned char* temp = (unsigned char*)event.GetClientData();
//do something with temp, which holds unsigned char* data from the thread
//GetClientData() can return any kind of data you want, but you have to cast it.
delete[] temp;
}
MyThread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <wx/thread.h>
#include <wx/event.h>
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(wxEVT_MYTHREAD, -1)
END_DECLARE_EVENT_TYPES()
class MyThread : public wxThread
{
public:
MyThread(wxEvtHandler* pParent, int param);
private:
int m_param;
void* Entry();
protected:
wxEvtHandler* m_pParent;
};
#endif
MyThread.cpp
#include "MyThread.h"
DEFINE_EVENT_TYPE(wxEVT_MYTHREAD)
MyThread::MyThread(wxEvtHandler* pParent, int param) : wxThread(wxTHREAD_DETACHED), m_pParent(pParent)
{
//pass parameters into the thread
m_param = param;
}
void* MyThread::Entry()
{
wxCommandEvent evt(wxEVT_MYTHREAD, GetId());
//can be used to set some identifier for the data
evt.SetInt(r);
//whatever data your thread calculated, to be returned to GUI
evt.SetClientData(data);
wxPostEvent(m_pParent, evt);
return 0;
}
我覺得這比維基提供的更清晰,簡潔的例子。顯然,我遺漏了有關實際啓動應用程序的代碼(wx約定會使MyApp.cpp)以及任何其他與線程無關的代碼。
0
如果你的程序很簡單,並且你不想亂搞線程,你可以考慮定期在你的long函數中調用wxWindow :: Update()。
2
從實施上述的一些技巧:
使用MINGW32和代碼塊,我有以下
warning: EVENT redeclared without dllimport attribute: previous dllimport ignored
。如果您不需要導出活動,請使用DEFINE_LOCAL_EVENT_TYPE
和DECLARE_LOCAL_EVENT_TYPE
(而不是DEFINE_EVENT_TYPE
和DECLARE_EVENT_TYPE
)。如果要通過
SetClientData()
傳遞對象,請確保在可拆卸線程中使用new
運算符創建數據。一旦複製數據,調用應用程序將必須複製delete
數據。
例如:
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_LOCAL_EVENT_TYPE(wxEVT_CALC_THREAD, -1)
END_DECLARE_EVENT_TYPES()
void* MyThread::Entry()
{
wxCommandEvent evt(wxEVT_CALC_THREAD, GetId());
// do some work
vector<map<int, int> > *vm = new vector<map<int, int> >();
// perform operations with the object vm ...
evt.SetClientData((void*)vm);
wxPostEvent(m_pParent, evt);
}
,並在調用應用程序:
DEFINE_LOCAL_EVENT_TYPE(wxEVT_CALC_THREAD)
// change this to your event table
BEGIN_EVENT_TABLE(..., ...)
EVT_COMMAND(wxID_ANY, wxEVT_CALC_THREAD, ThreadDone)
END_EVENT_TABLE()
void ThreadDone(wxCommandEvent& event)
{
vector<map<int, int> > *temp = (vector<map<int, int> > *)event.GetClientData();
// store the data in *temp
delete temp;
}
相關問題
- 1. 的wxWidgets和線程
- 2. 轉換在windows線程中寫入的代碼到wxwidgets線程
- 3. 在wxwidgets中的線程內運行長進程
- 4. wxWidgets GUI應用程序中的多線程?
- 5. GUI在多線程應用程序的Wxwidgets中沒有響應
- 6. WxWidgets運行時線程崩潰
- 7. 如何避免C++中的多線程問題(wxWidgets和Cplex)?
- 8. wxWidgets的 - 退出線程的正確方法
- 9. 在wxWidgets中
- 10. 在wxwidgets中,我如何鎖定在gui線程和工作線程之間共享的向量?
- 11. C++ WxWidgets:通過多個線程將Stdout重定向到wxTextCtrl
- 12. 在使用wxWidgets的另一個線程中顯示進度指示器
- 13. wxWidgets的C++中的窗戶
- 14. 在wxwidgets中,如何讓一個線程在繼續之前等待另一個線程完成?
- 15. 在windows中的wxwidgets錯誤
- 16. wxWidgets中的錯誤處理
- 17. wxWidgets中的wxGrid問題2.9.1
- 18. wxWidgets中的wxGetElapsedTime函數2.9+
- 19. WxWidgets中的緩衝圖像
- 20. 使用DLL中的wxWidgets
- 21. wxWIdgets中的.NET Encoding.Default選項?
- 22. wxBusyInfo wxWidgets中的替代2.8
- 23. 「未處理的異常」 錯誤混合的boost ::線程時與wxWidgets的GUI
- 24. wxWidgets的是OnInit
- 25. wxWidgets的 - wxThread
- 26. wxWidgets的應用程序運行失敗
- 27. C++ WxWidgets:來自多個線程的消息的單個日誌窗口
- 28. wxtooltip wxwidgets
- 29. wxWidgets wxBitmap
- 30. 線程池中的線程
這可能有助於 http://docs.wxwidgets.org/trunk/classwx_thread_helper.html – Aif 2010-06-01 20:32:55
這... http://wiki.wxwidgets.org/Inter-Thread_and_Inter-Process_communication – laher 2010-06-01 21:19:18