Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
Posted
Updated 9-Jul-10 11:01am
v3

1 solution

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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900