简体   繁体   English

使用wxthread在C ++中更新wxframe的内容

[英]Use wxthread to update content of wxframe in C++

I am a newbie in using threads. 我是使用线程的新手。 I have developed a program in C++ using wxwidgets to show the count of packets received through the network interface. 我使用wxwidgets开发了C++程序,以显示通过网络接口接收到的数据包的数量。 What I have done so far is: I have a wxbutton in a wxdialog . 到目前为止,我所做的是:我在wxbutton中有一个wxdialog On clickng on the button a wxframe containing wxgrid is opened. 上的按钮clickng一个wxframe含有wxgrid被打开。 The wxgrid contains info like interface name, packets in and packets out. wxgrid包含诸如接口名称,输入和输出数据包之类的信息。

Now what I need is to constantly update the packets in and packets out fields. 现在,我需要不断更新数据包输入和输出数据包字段。 I have a function that calculates the packets in and packets out. 我有一个函数,可以计算入包和出包。 So I think I need to execute the function continuously until the user closes the frame . 所以我认为我需要连续执行功能,直到用户关闭frame为止。

I hope wxthread can be utilized to perform this. 我希望可以利用wxthread来执行此操作。 But have no idea on how to start with or how a function can be executed from a thread. 但是对如何开始或如何从线程执行功能一无所知。 Can someone please guide me.. THANK YOU 有人可以指导我吗。谢谢

A worker thread can't access any GUI object directly, so you need to post events to the main thread where you can define event handlers for them which will do whatever you need. 辅助线程无法直接访问任何GUI对象,因此您需要将事件发布到主线程,在该线程中您可以为它们定义事件处理程序,该处理程序可以完成您所需的任何操作。 There is a convenient base class for such events called wxThreadEvent that you may find useful. 对于此类事件,有一个方便的基类称为wxThreadEvent ,您可能会发现它很有用。

Alternatively, and especially if you use C++11, you could use CallAfter() which allows you to execute a callback in the main thread context. 另外,特别是如果您使用C ++ 11,则可以使用CallAfter() ,它允许您在主线程上下文中执行回调。 This is especially nice with C++11 lambdas because it allows you to keep all your code in the same place without having to extract it into a separate event handler. 这对于C ++ 11 lambda尤其有用,因为它允许您将所有代码保留在同一位置,而不必将其提取到单独的事件处理程序中。

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

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