Board index » Visual Studio » UI Thread and CSocket

UI Thread and CSocket

Visual Studio325
Hi All,



I have created a UI Thread with a CSocket object inside.



The thread was created with SUSPENDED state.



But the Socket is still able to receive incoming request even when I

didn't ResumeThread.



When I resume the thread later, the state return is '1', that shows it

was indeed suspended.



So, my question is, when a UI thread is suspended, is it mean the

message pump still running?


-
 

Re:UI Thread and CSocket

Kwan wrote:

Quote
Hi All,



I have created a UI Thread with a CSocket object inside.



The thread was created with SUSPENDED state.



But the Socket is still able to receive incoming request even when I

didn't ResumeThread.



When I resume the thread later, the state return is '1', that shows it

was indeed suspended.



So, my question is, when a UI thread is suspended, is it mean the

message pump still running?



No. The message pump definitely does not run when a UI thread is

suspended. What function calls CSocket::Create? Have you studied the

MFC multithread socket example code (MFCAsync)? The socket uses the

message pump of the thread that called Create.



--

Scott McPhillips [VC++ MVP]



-

Re:UI Thread and CSocket

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>wrote in message news:<evzXFC2gFHA.3912@tk2msftngp13.phx.gbl>...



Quote
No. The message pump definitely does not run when a UI thread is

suspended. What function calls CSocket::Create? Have you studied the

MFC multithread socket example code (MFCAsync)? The socket uses the

message pump of the thread that called Create.



No, I didn't use Async socket. This is an CSocket object, and it was

working without much problem previously.



Now I have to move it into a UI Thread. What I did was:



AfxBeginThread( ....... , CREATE_SUSPENDED);



Within that CWinThread object, there is an instance of the CSocket.

And I call the Create() and Listen().



Strange thing happened, the Socket will be able to accept in-coming

message, even when the CWinThread is (still suspended) not Resume. :(



This is not really an issue, but one can imagine that if things happen

very fast, there might have some threading issue. What will happen if

that CWinThread received a WM_QUIT before I call Create() / Listen()?



Do you have any clue?

-

Re:UI Thread and CSocket

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>wrote in message news:<evzXFC2gFHA.3912@tk2msftngp13.phx.gbl>...



Quote
MFC multithread socket example code (MFCAsync)? The socket uses the

message pump of the thread that called Create.



Ohhh... after I have posted my previous message then only I notice you

had given me the answer :P:P:P:P



Yes, the message pump of the thread that called Create().... :(:(



seemsl ike I need to re-design the code.

-

Re:UI Thread and CSocket

You have not indicated how the CSocket was created. There are two considerations here:



First, forget CSocket exists. It is the worst possible way to write networking code, it is

an unreliable method of doing network communication. And it has been reported that the MFC

implementation of CSocket has bugs. So you should always use CAsyncSocket.



Second, you have not said anything about how this CSocket gets created and opened. The

implication of your question is that it has already been opened, presumably in the main

thread. In that case, the main thread is handling the traffic, so the fact that there is a

object of type CSocket contained in a CWinThread object is pretty irrelevant. Just because

the object is stored in some random memory location such as inside a CWinThread does not

affect its behavior if it already belongs to some other thread. You cannot pass a

C[Async]Socket across thread boundaries, and consequently you need to pass the raw SOCKET

object across the boundary. To do this, see my essay on UI threads on my MVP Tips site,

which discusses how to create a socket in one thread and pass it across to another thread.



What it means is that you probably have a CSocket owned by the main GUI thread, so the

state of the UI thread is absolutely irrelevant to the CSocket or its behavior.

joe



On 7 Jul 2005 18:19:37 -0700, ckkwan@my-deja.com (Kwan) wrote:



Quote
Hi All,



I have created a UI Thread with a CSocket object inside.



The thread was created with SUSPENDED state.



But the Socket is still able to receive incoming request even when I

didn't ResumeThread.



When I resume the thread later, the state return is '1', that shows it

was indeed suspended.



So, my question is, when a UI thread is suspended, is it mean the

message pump still running?



Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:UI Thread and CSocket

BTW Scott,



How about Accept? Will the socket uses the message pump of the thread

which called the Accept?



It seems like this is not very convenient to program in this way.



Thanks in advance

-

Re:UI Thread and CSocket

Kwan wrote:

Quote
BTW Scott,



How about Accept? Will the socket uses the message pump of the thread

which called the Accept?



It seems like this is not very convenient to program in this way.



Thanks in advance



It is not very convenient to "move" a CSocket or CAsyncSocket from one

thread to another because the class is dependant on the MFC message

pump, which is thread-specific. There are MFC sample programs in the

help and at MSDN online that show exactly how to do it.



--

Scott McPhillips [VC++ MVP]



-