调试断言失败了...... [英] Debug assertion failed...

查看:95
本文介绍了调试断言失败了......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

THREADSTRUCT* ts = (THREADSTRUCT*)param;



ts->_this->OnInitDialog();



这里的OnInitDialog()在主线程中,我试图从我创建的子线程(工作线程)调用此方法。当我尝试这样做时,我得到一个调试断言失败错误(在调试模式和发布模式下)。

**我使用的是Microsoft Visualc ++ 6.0 IDE **


Here OnInitDialog() in in main thread and i am trying to call this method from the child thread(worker thread) which i have created. when i am trying to do this i am getting a debug assertion failed error(Both in Debug mode and release mode).
** I am using Microsoft Visualc++ 6.0 IDE**

推荐答案

MFC对象不是线程安全的。所以你不能从另一个线程调用像 OnInitDialog 这样的MFC函数。



这个问题的一般解决办法是将用户定义的消息从工作线程发布到另一个(主)线程。



作为起点,您可以阅读多线程阅读:编程技巧 [ ^ ]并点击链接。自VC 6以来,这一点没有太大变化。但是你应该考虑使用更新的版本。
MFC objects are not thread safe. So you can't call MFC functions like OnInitDialog from another thread.

A usual solution to this problem is posting user defined messages from the worker thread to the other (main) thread.

As a starting point you may read Multithreading: Programming Tips[^] and follow the links. This has not changed much since VC 6. But you should think about using a more recent version.


这不是线程安全问题。 MFC保存MFC对象的窗口句柄映射,用于将传入的Windows消息路由到相应的C ++类对象。此映射是特定于线程的,只能从创建窗口的线程访问。因此,您可能只处理创建它们的线程中的窗口和控件。一种常见的技术是对所有UI活动使用单个线程,对所有非UI活动使用工作线程。



这也是你必须在MFC中指定的原因是否要创建UI或工作线程。
This is less a problem of thread-safeness. MFC keeps a map of window handles to MFC objects, which is used to route an incoming windows message to the corresponding C++ class object. This map is thread-specific and can only be accessed from the thread in which the window was created. Hence, you may only deal with windows and controls from the thread that created them. A common technique is to use a single thread for all UI activities and worker threads for all non-UI activities.

That is also the reason why you must specify in MFC whether you want to create a UI or worker thread.


这篇关于调试断言失败了......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆