简体   繁体   English

在Windows上的C ++中的wxwdigets中使用和创建线程

[英]Using & Creating threads in wxwdigets in c++ on windows

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:- 我正在阅读与在Windows上的C ++中使用wxwidgets创建线程的相关知识,但是我无法理解以下代码片段的含义:-

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 谢谢

Firstly I would suggest checking out the threads sample in your wxWidgets install and the documentation page for wxThread which includes a large example. 首先,我建议您检查一下wxWidgets安装中的线程示例以及wxThread的文档页面,其中包括一个大型示例。 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. 这是一个事件表 ,它表示该框架将处理具有任何ID的wxEVT_MYTHREAD类型的事件,并且在接收到该事件时将调用MyFrm::OnMyThread方法。

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. 此方法创建一个MyThread实例,并为其传递一个整数参数,以进行一些计算。 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. 触发wxEVT_MYTHREAD事件时,将调用此方法。 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() 在此示例中,更令人困惑的是,它不包含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. 我将看一下在wxWidgets中对此进行解释的wxApp概述以及仅显示wxWidgets程序基础的最小样本。

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

This code declares a new event type called wxEVT_MYTHREAD . 此代码声明了一个名为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. Entry方法在wxThread中完成实际工作,有关更多详细信息,请参见文档 In this case it creates an event sets the parameters and then posts it. 在这种情况下,它将创建一个事件并设置参数,然后发布该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM