在另一个线程中无法用((CMainFrame *)AfxGetMainWnd())的解决方法
2018-03-30 21:39
531 查看
一个vc6的项目放到vc8下重新编译这里死活过不去 查了些资料无果后来翻到一句老外的回答If AfxGetMainWnd is called from the application’s primary thread, it returns the application’s main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.大概意思就是说在子线程里面调用AfxGetMainWnd()返回的是和当前线程相关联的窗体句柄而不是当前程序的主窗体句柄。不知道这是不是vc8的一个改变,也没时间去细查。解决方法1:
CWnd* m_pCWnd = NULL;
在OnInitDialog里 m_pCWnd = AfxGetMainWnd();解决方法2:
调用AfxGetMainWnd()的地方替换成AfxGetApp()->m_pMainWnd编译后运行问题解决我用方法2解决问题。 AfxGetMainWnd()得到的是当前线程的主窗口(如果有的话).
因为主窗口是属于主线程的,所以想得到主窗口HWND值,
只能在主线程中用AfxGetMainWnd(),但要不是处在主线程中,
AfxGetMainWnd()可能是从当前线程查询主窗口的。但好像
AfxGetMainWnd()不能跨线程,故要出错.要想在线程中使用
主窗口的HWND值,可以把主窗口的HWND值传给线程.也
可以用AfxGetApp()先取得主线程,再通过CWinThread的类成
员m_pMainWnd获得主窗口(AfxGetApp()->m_pMainWnd->m_hWnd)
If AfxGetMainWnd is called from the application's primary thread,
it returns the application'smain window according to the above rules.
If the function is called from a secondary thread in the application,
the function returns the main window associated with the thread that made the call.
今天好像找到原因了~ 我那个线程是用CreateThread创建的,而 CreateThread是由
操作系统提供的接口,在 CreateThread创建的线程中用MFC的函数AfxGetMainWnd()会有问题. 如果用MFC编程,不要用CreateThread,如果只是使用Runtime Library,用
_BeginThread,总之,不要轻易使用CreateThread
这是因为在MFC和RTL中的函数有可能会用到些它们所封装的公用变量,也就是
说AfxBeginThread和_BeginThread都有自己的启动代码是CreateThread所没有的
在用CreateThread所创建的线程中使用MFC的类和RTL函数就有可能出现问题
如果你是用汇编编写win32程序并且在线程函数中也不调用MFC和RTL的函数,那用
CreateThread就没问题,或者你虽然是用C写线程函数,但你很小心没调用RTL函数
也不会有问题
CreateThread是由操作系统提供的接口,而AfxBeginThread和_BeginThread则是编译
器对它的封装
转自https://www.cnblogs.com/ct0421/p/4318232.html
CWnd* m_pCWnd = NULL;
在OnInitDialog里 m_pCWnd = AfxGetMainWnd();解决方法2:
调用AfxGetMainWnd()的地方替换成AfxGetApp()->m_pMainWnd编译后运行问题解决我用方法2解决问题。 AfxGetMainWnd()得到的是当前线程的主窗口(如果有的话).
因为主窗口是属于主线程的,所以想得到主窗口HWND值,
只能在主线程中用AfxGetMainWnd(),但要不是处在主线程中,
AfxGetMainWnd()可能是从当前线程查询主窗口的。但好像
AfxGetMainWnd()不能跨线程,故要出错.要想在线程中使用
主窗口的HWND值,可以把主窗口的HWND值传给线程.也
可以用AfxGetApp()先取得主线程,再通过CWinThread的类成
员m_pMainWnd获得主窗口(AfxGetApp()->m_pMainWnd->m_hWnd)
If AfxGetMainWnd is called from the application's primary thread,
it returns the application'smain window according to the above rules.
If the function is called from a secondary thread in the application,
the function returns the main window associated with the thread that made the call.
今天好像找到原因了~ 我那个线程是用CreateThread创建的,而 CreateThread是由
操作系统提供的接口,在 CreateThread创建的线程中用MFC的函数AfxGetMainWnd()会有问题. 如果用MFC编程,不要用CreateThread,如果只是使用Runtime Library,用
_BeginThread,总之,不要轻易使用CreateThread
这是因为在MFC和RTL中的函数有可能会用到些它们所封装的公用变量,也就是
说AfxBeginThread和_BeginThread都有自己的启动代码是CreateThread所没有的
在用CreateThread所创建的线程中使用MFC的类和RTL函数就有可能出现问题
如果你是用汇编编写win32程序并且在线程函数中也不调用MFC和RTL的函数,那用
CreateThread就没问题,或者你虽然是用C写线程函数,但你很小心没调用RTL函数
也不会有问题
CreateThread是由操作系统提供的接口,而AfxBeginThread和_BeginThread则是编译
器对它的封装
转自https://www.cnblogs.com/ct0421/p/4318232.html
相关文章推荐
- 在另一个线程中无法用((CMainFrame *)AfxGetMainWnd())
- VC2008下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- VC8下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- VC8下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- vs2010中,在线程中,调用SendMessage(AfxGetMainWnd()->m_hWnd,...)异常的问题
- AfxGetMainWnd()在线程里使用的问题
- 前台线程与后台线程,AfxGetApp->GetMainWnd()与AfxGetMainWnd的不同 (转)
- 关于在线程中使用AfxGetMainWnd()出错的问题,终于找到了
- 用了invoke还是出错的解决方法.wpf 调用线程无法访问此对象,因为另一个线程拥有该对象。
- 关于在线程中使用AfxGetMainWnd()出错的问题,终于找到了
- 前台线程与后台线程,AfxGetApp->GetMainWnd()与AfxGetMainWnd的不同
- 细微区别:视图中GetParentFrame 和 AfxGetMainWnd
- 关于在线程中使用AfxGetMainWnd()出错的问题,终于找到了
- 【框架-MFC】MFC 应用程序 AfxGetMainWnd();在子线程中返回null
- MFC线程里调用AfxGetMainWnd的陷阱
- 【多线程与AfxGetMainWnd & 窗口 与 线程 有必然联系吗? & 命名空间】
- WPF中调用线程无法访问此对象,因为另一个线程拥有该对象 的解决方法
- 在线程中执行AfxGetMainWnd()出错的问题
- 关于在线程中使用AfxGetMainWnd()出错的问题
- afxgetapp->afxmainwnd 与afxgetmainwnd的区别 /线程中调用afxgetmainwnd的问题