Click here to Skip to main content
13,089,386 members (58,534 online)
Rate this:
Please Sign up or sign in to vote.
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., my question (finally) 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, 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...

Posted 8-Jul-10 10:08am
Updated 9-Jul-10 11:01am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?

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

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 15 Sep 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100