Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0 VS2005 C++ MFC
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 8-Jul-10 11:08am
kinar5.6K
Edited 9-Jul-10 12:01pm
v3

1 solution

Rate this: bad
good
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?
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 260
1 Mathew Soji 244
2 Afzaal Ahmad Zeeshan 232
3 OriginalGriff 231
4 Maciej Los 230
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 15 Sep 2011
Copyright © CodeProject, 1999-2014
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