When working with threads, you often find yourself coding the same code again and again (derive from wxThread, implement Entry() bla bla bla)
attached you can find my implementation of JobQueue - a classes which manages pool of threads that allows applications to perform background tasks easily with minimum code
sample.cpp which shows how to use JobQueue
Code: Select all
class MyJob : public Job {
public:
MyJob(){}
~MyJob(){}
// Implement your processing code here
void Process() {
wxPrintf(wxT("Just doing my work...\n"));
};
};
// define some custom job with progress report
class MyJobWithProgress : public Job {
public:
MyJobWithProgress(wxEvtHandler *parent) : Job(parent){}
~MyJobWithProgress(){}
// Implement your processing code here
void Process() {
// report to parent that we are at stage 0
Post(0, wxT("Stage Zero"));
// do the work
wxPrintf(wxT("Just doing my work...\n"));
// report to parent that we are at stage 1
Post(1, wxT("Stage Zero Completed!"));
};
};
// somewhere in your code, start the JobQueue
// for the demo we use pool of size 5
JobQueueSingleton::Instance()->Start(5);
// whenever you want to process MyJob(), just create new instance of MyJob() and add it to the JobQueue
JobQueueSingleton::Instance()->AddJob( new MyJob() );
// at shutdown stop the job queue and release all its resources
JobQueueSingleton::Instance()->Stop();
JobQueueSingleton::Release();
// OR, you can use JobQueue directly without the JobQueueSingleton wrapper class
// so you could have multiple instances of JobQueue
Code: Select all
// in the event table
EVT_COMMAND(wxID_ANY, wxEVT_CMD_JOB_STATUS, MyFrame::OnJobStatus)
void MyFrame::OnJobStatus(wxCommandEvent &e)
{
wxString msg;
msg << wxT("Job progress: Stage: ") << e.GetInt() << wxT(" Message: ") << e.GetString();
wxLogMessage(msg)
}