CWinThread"UI线程",CAsyncSocket和消息泵 [英] CWinThread "UI Thread", CAsyncSocket, and message pumps

查看:79
本文介绍了CWinThread"UI线程",CAsyncSocket和消息泵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个MFC Dialog应用程序,该应用程序创建一个新线程(从CWinThread派生),该线程创建一个套接字(从CASyncSocket派生).这样的想法是将套接字通信与Dialog线程隔离.

当我想通知线程类处理已接收的数据时,我尝试从套接字类中使用PostThreadMessage.然后,每当对话框需要通知用户已检索到信息时,线程类便会使用PostMessage()通知对话框(当然,该线程位于其他线程中).

问题是创建套接字后,线程的消息泵似乎停止工作.我可以从线程内成功使用PostThreadMessage,直到创建套接字为止,此时我的处理程序再也不会触发.

所以...现在,我的问题(最终)是...我的设计有根本​​上的错误吗?还是我不了解CAsyncSocket的内部运作,这会导致我在此设计中遇到问题?

-编辑添加-
现在,我创建了一个测试应用程序,在其中我整理了以下代码:

1)创建了一个新的MFC对话框应用程序(取消选中unicode,检查Windows套接字).
2)将按钮和列表框添加到主应用程序的对话框
3)添加从CWinThread派生的MFC类
4)添加从CAsyncSocket派生的MFC类
5)添加按钮的OnClick处理程序,并在其中创建我的线程类的新实例,并使用CreateThread()
对其进行初始化 6)在线程的InitInstance处理程序中创建我的套接字类的实例.
7)在OnReceive()中添加简单地向线程触发消息的套接字通信代码(使用AfxGetThread()-> PostThreadMessage()). 8)将处理程序添加到我的用户消息的线程类中,该处理程序使用(AfxGetApp()-> GetMainWnd()-> PostMessage())
将消息发布到对话框线程 9)将处理程序添加到第二条用户消息的主dlg,并在列表框中显示一条消息已收到的通知.
10)编译,运行,单击,成功,就可以了....

好的,总而言之,测试应用程序可以正常工作,主应用程序无法工作...但是我不知道是什么原因导致主应用程序中的消息泵在CSocket出现后立即停止工作已创建...

OK, I have a MFC Dialog app which creates a new thread (derived from CWinThread) which creates a socket (derived from CASyncSocket). The idea of this is to isolate the socket communications from the Dialog thread.

I''m trying to use PostThreadMessage from within the socket class when I want to notify the thread class to process data that has been received. The thread class would then notify the dialog (which is of course in a different thread) using a PostMessage() whenever the dialog needs to give notice to the user that information has been retrieved.

The problem is that the Message Pump for the Thread seems to stop working as soon as the socket is created. I can use PostThreadMessage from within the thread successfully up until the point where I create the socket and at that point, my handler never fires again.

So...now, my question (finally) is...is there something fundamentally wrong with my design? Or is there simply something I don''t know about the inner workings of CAsyncSocket that is going to cause me problems with this design?

-edit to add-
I have now created a test app where I trimmed down the code doing the following:

1) Created a new MFC Dialog App (uncheck unicode, check windows sockets).
2) Add button and listbox to Dialog of the main app
3) Add MFC Class derived from CWinThread
4) Add MFC Class derived from CAsyncSocket
5) Add OnClick handler for button and in it, I create a new instance of my thread class on the heap and initialize it using CreateThread()
6) Create an instance of my socket class in the InitInstance handler of the thread.
7) add socket communications code that simply fires a message to the thread (using AfxGetThread()->PostThreadMessage()) in the OnReceive()
8) Add handler to thread class for my user message which posts a message to the dialog thread using (AfxGetApp()->GetMainWnd()->PostMessage())
9) Add handler to main dlg for the 2nd user message and displays a notification in the listbox that the message was recieved.
10) compile, run, click, success, it works....

OK, so...to make a long story short, Test App works, main app doesn''t work...but I have no idea what would cause the message pump in my main app to stop working as soon as the CSocket is created...

ideas?

推荐答案

1.我总是从beginthreadex开始线程,以防从线程中的代码调用C运行时库.
2.您是否曾经在线程开始以确保为该线程创建消息队列时调用PeekMessage()?
1. I always start threads with beginthreadex in case the C runtime library gets called from code within the thread.

2. Did you ever call PeekMessage() when the thread starts to make sure that a message queue is created for the thread?


这篇关于CWinThread"UI线程",CAsyncSocket和消息泵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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