0

I was reading related to the cration of threads using wxwidgets in c++ on windows however i am not able to understand the what the following snippet of the code means:-

the following things are a part of the project:-

  Myfirm.cpp
  My thread.h
  Mythread.cpp

  in Myfirm.cpp

  the following code is not understood by me:-

  BEGIN_EVENT_TABLE(MyFrm,wxFrame)
       EVT_COMMAND(wxID_ANY, wxEVT_MYTHREAD, MyFrm::OnMyThread)
  END_EVENT_TABLE()

  void MyFrm::PerformCalculation(int someParameter){//not sure what is it 
   MyThread *thread = new Mythread(this, someParameter);
   thread->Create();
   thread->Run();
   }

  void MyFrm::OnMyThread(wxCommandEvent& event)//here also the clarity is not good
  {
   unsigned char* temp = (unsigned char*)event.GetClientData();

   delete[] temp;  
  }    

  in this exampe what is even more confusing is that , it does not contain a main()     
  function

  in Mythread.h

  BEGIN_DECLARE_EVENT_TYPES()
       DECLARE_EVENT_TYPE(wxEVT_MYTHREAD, -1)
  END_DECLARE_EVENT_TYPES()

  in Mythread.cpp

  DEFINE_EVENT_TYPE(wxEVT_MYTHREAD)
  MyThread::MyThread(wxEvtHandler* pParent, int param) : wxThread(wxTHREAD_DETACHED),                      
  m_pParent(pParent)

  {
   m_param = param;
  }
  void* MyThread::Entry()
  {
  wxCommandEvent evt(wxEVT_MYTHREAD, GetId());
  evt.SetInt(r); 
  evt.SetClientData(data); 
  wxPostEvent(m_pParent, evt);
  return 0;
  }
  i am atill wondering how does the following code works and have no idea about where 
  where the main function is?

Thanks

  • In wxWidget applications there isn't a `main` function you write yourself, instead it's wrapped in a special wxWidget macro `IMPLEMENT_APP`. What more specific don't you understand? How wxWidget threads works? Forms? Events? Have you read the manuals and tutorials? – Some programmer dude Apr 13 '12 at 05:20
  • I was using this as an example to use it in my qwn project, i know that one needs to define a macro like #define IMPLEMENT_APP,but its relevancy here and use i am not able to guess,suggest its use here as well. – james whitehurst Apr 13 '12 at 05:28

1 Answers1

1

Firstly I would suggest checking out the threads sample in your wxWidgets install and the documentation page for wxThread which includes a large example. However let me break down your code and try to explain it.

BEGIN_EVENT_TABLE(MyFrm,wxFrame)
       EVT_COMMAND(wxID_ANY, wxEVT_MYTHREAD, MyFrm::OnMyThread)
END_EVENT_TABLE()

This is an event table, it says that the frame will handle events of type wxEVT_MYTHREAD with any ID and when it receives one it will call the MyFrm::OnMyThread method.

void MyFrm::PerformCalculation(int someParameter){//not sure what is it 
   MyThread *thread = new Mythread(this, someParameter);
   thread->Create();
   thread->Run();
}

This method creates an instance of MyThread passing it an integer parameter, presumably to do some calculations on. It then runs the thread.

void MyFrm::OnMyThread(wxCommandEvent& event)//here also the clarity is not good
{
   unsigned char* temp = (unsigned char*)event.GetClientData();
   delete[] temp;  
} 

This is the method that is called when a wxEVT_MYTHREAD event is fired. It then gets the data from the event (that in our case was set in the thread) and would normally do something with it, in this case it just deletes it.

in this exampe what is even more confusing is that , it does not contain a main()
function

I would take a look at the wxApp overview which explains this in wxWidgets, as well as the minimal sample which only shows the basics of a wxWidgets program.

BEGIN_DECLARE_EVENT_TYPES()
       DECLARE_EVENT_TYPE(wxEVT_MYTHREAD, -1)
END_DECLARE_EVENT_TYPES()

This code declares a new event type called wxEVT_MYTHREAD.

DEFINE_EVENT_TYPE(wxEVT_MYTHREAD)

This defines the new event type.

MyThread::MyThread(wxEvtHandler* pParent, int param) : wxThread(wxTHREAD_DETACHED), m_pParent(pParent)
{
    m_param = param;
}

The constructor for the thread class that stores the parameter.

void* MyThread::Entry()
{
    wxCommandEvent evt(wxEVT_MYTHREAD, GetId());
    evt.SetInt(r); 
    evt.SetClientData(data); 
    wxPostEvent(m_pParent, evt);
    return 0;
}

The Entry method does the actual work in a wxThread, see the documentation for more details. In this case it creates an event sets the parameters and then posts it.

SteveL
  • 1,811
  • 1
  • 13
  • 23