[wxWidgets]_[初级]_[发送异步事件的注意项之字符串深浅复制]
来源:互联网 发布:vscode 打开sln 编辑:程序博客网 时间:2024/05/12 04:08
1.在工作线程和主线程进行通讯时,一般是通过发送事件来同步数据,而工作线程只能发送异步事件,基本不能发送界面绘制相关的同步事件。通讯数据时大多情况下需要传递字符串数值,这时候就需要先使用wxEvent的SetString方法,再使用QueueEvent.这里注意了,异步事件是对wxString有所有权的.
以下摘录wx.chm文档里的api说明.
QueueEvent() can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent() documentation by ensuring that the event object is not used by the calling thread any more. Care should still be taken to avoid that some fields of this object are used by it, notably any wxString members of the event object must not be shallow copies of another wxString object as this would result in them still using the same string buffer behind the scenes. For example:
wxCommandEvent 普通事件:
void FunctionInAWorkerThread(const wxString& str){wxCommandEvent* evt = new wxCommandEvent;// NOT evt->SetString(str) as this would be a shallow copy 浅复制,就是复制了计数器evt->SetString(str.c_str()); // make a deep copy 深复制,连数据一起创建了一个copy.wxTheApp->QueueEvent( evt );}
wxThreadEvent 线程事件:
void FunctionInAWorkerThread(const wxString& str){wxThreadEvent evt;evt->SetString(str);// wxThreadEvent::Clone() makes sure that the internal wxString// member is not shared by other wxString instances:wxTheApp->QueueEvent( evt.Clone() );}
2.wxString里面其实就是std::string,用std::string时也要注意点。
string( const string& s ); // a copy of the given string s, 浅复制string( const char* str ); // a duplicate of str (optionally up to length characters long), 深复制
- [wxWidgets]_[初级]_[发送异步事件的注意项之字符串深浅复制]
- [wxWidgets]_[初级]_[反转wxString字符串]
- [wxWidgets]_[初级]_[监听wxListCtrl和wxGrid的滚动条事件]
- [wxWidgets]_[初级]_[常见更新进度条的方案]
- [wxWidgets]_[初级]_[自定义按钮]
- [wxWidgets]_[初级]_[正则表达式]
- [wxWidgets]_[初级]_[wxWidgets-2.9.2编译参数]
- C++中的深浅复制_实践示例
- [wxWidgets]_[初级]_[简单应用看wx的核心原理]
- [wxWidgets]_[初级]_[不以界面程序的方式使用wx库]
- [wxWidgets]_[初级]_[自绘窗口最小化点击任务栏图标还原的办法]
- [wxWidgets]_[初级]_[Mac OS X下添加输入框的右键菜单]
- [wxWidgets]_[初级]_[不常见但实用的类wxCmdLineParser]
- [wxWidgets]_[初级]_[不常见但实用的类wxStandardPaths]
- [wxWidgets]_[初级]_[wxThread使用简单介绍]
- [wxWidgets]_[初级]_[Debug Assertion Failure: BLOCK_TYPE_IS_VALID(pHead->nBlockUse) ]
- [wxWidgets]_[初级]_[配置codeblock+wxWidgets3.0.1开发环境]
- [Cocoa]_[初级]_[如何复制NSView]
- Json数据
- spring framework 基础知识
- CentOS 6.3下vsftpd的简单使用教程
- Codeforces_C - Beautiful Numbers
- CentOS 6.3设置静态IP教程
- [wxWidgets]_[初级]_[发送异步事件的注意项之字符串深浅复制]
- CentOS上yum安装Nginx服务
- CentOS内核编译
- centos 5.6 mysql远程访问设置(无需密码)
- C++ 迭代器失效
- centos删除多余内核启动项
- Centos5.7 中文乱码问题解决方案
- 串口接收时丢数据问题解决方案
- CCSprite更换图片