From: Scott McPhillips [MVP] on
Joel wrote:
> Ok thanks for help...
>
> Let see how i do:
>
> To launch my thread:
> TacheEcoute* tacheEc = (TacheEcoute*) // This is my CWinThread
>
> AfxBeginThread(RUNTIME_CLASS(TacheEcoute), THREAD_PRIORITY_NORMAL, 0,
> CREATE_SUSPENDED);
>
> tacheEc->setSocket(s.Detach());
>
> tacheEc->ResumeThread();
>
>
>
> I have an infinite loop in my ::Run() function
>
>
> And my pumpmessage definitivly don t work. I crash the first time I do it
> (but I need to overload my app to have to do it).
>
>
> Do you see what am I doing wrong ?
>
> Thanks again for help.
>
> Joel

The CWinThread::Run function contains the message loop. If you are not
calling it within your loop then you don't have a message loop and the
socket will not receive its notifications.

The MultiSoc sample in MSDN does what you are trying to do. Compare
your code with that.

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
The error is in overriding the Run() function. Generally, you can assume that if you do
this, you have made a fundamental design error. There is no reason to override the Run()
function. Since you overrode the Run() function, and put an infinite loop in it, you have
blocked the message pump. End of story. It won't work.

If your thread is being driven by input messages, then it has no need to override the
message loop. If it is not, there is a good chance it should not be a UI thread. What is
the "infinite loop" actually doing?
joe

On Mon, 28 Feb 2005 09:05:27 +0100, "Joel" <nospam(a)nospam.com> wrote:

>Ok thanks for help...
>
>Let see how i do:
>
>To launch my thread:
>TacheEcoute* tacheEc = (TacheEcoute*) // This is my CWinThread
>
>AfxBeginThread(RUNTIME_CLASS(TacheEcoute), THREAD_PRIORITY_NORMAL, 0,
>CREATE_SUSPENDED);
>
>tacheEc->setSocket(s.Detach());
>
>tacheEc->ResumeThread();
>
>
>
>I have an infinite loop in my ::Run() function
>
>
>And my pumpmessage definitivly don t work. I crash the first time I do it
>(but I need to overload my app to have to do it).
>
>
>Do you see what am I doing wrong ?
>
>Thanks again for help.
>
>Joel
>
>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>news:d6l42198eq59lfecjjkmolh2qgleh1p4va(a)4ax.com...
>> With a CWinThread there are a couple issues about how it is created. For
>example,
>> AfxBeginThread(threadfunc, parameter) returns a CWinThread object, but
>does not create a
>> UI thread. I've also see AfxBeginThread done on a CWinThread-derived
>subclass but all the
>> work is done in an infinite loop in InitInstance, which means there is no
>functioning
>> message pump. Let me rephrase it: you have to have a UI thread with a
>functioning message
>> pump for a socket to work at all. But CSocket is really risky. I never
>used one, so I
>> can't attest to the claims that the implementation is buggy, but there are
>so many
>> fundamental problems with synchronous sockets as a communication mechanism
>(even if they
>> worked properly) that I would never use one.
>> joe
>>
>> On Sat, 26 Feb 2005 16:08:31 +0100, "Crosoft" <trash(a)trash.fr> wrote:
>>
>> >"Joseph M. Newcomer" <newcomer(a)flounder.com> a ýcrit dans le message de
>> >news: eqqu119a6c9021107ifuajlf305jpgo0tc(a)4ax.com...
>> >> Using CSocket is a Really Bad Idea. Not only are synchronous sockets
>Bad
>> >Ideas, but the
>> >> rumor is that the CSocket implementation has bugs. Rewrite your code to
>> >use CAsyncSocket.
>> >>
>> >> Is your thread a UI thread? If it is not a UI thread, no socket can
>work.
>> >
>> >CWinThread ....
>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>

Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joel on

"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:gmh82113angtrv0ljh77pjk7kls92qmoqp(a)4ax.com...
> The error is in overriding the Run() function. Generally, you can assume
that if you do
> this, you have made a fundamental design error. There is no reason to
override the Run()
> function. Since you overrode the Run() function, and put an infinite loop
in it, you have
> blocked the message pump. End of story. It won't work.
Ok I was not aware of this...

But i don't see why i crash here :

BOOL CSocket::PumpMessages(UINT uStopFlag)
{
// The same socket better not be blocking in more than one place.
ASSERT(m_pbBlocking == NULL);

_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;

ASSERT(pState->m_hSocketWindow != NULL); <---- HERE

Really, i don't see...


>
> If your thread is being driven by input messages, then it has no need to
override the
> message loop. If it is not, there is a good chance it should not be a UI
thread. What is
> the "infinite loop" actually doing?

int TacheEcoute::Run()
{
if (!AfxSocketInit())
{ AfxMessageBox("Socket Error");
}

// Some initialization
vs=new VirtualSession(&sock,fenetre);
VIRTUALSESSION=vs;
MSG message;


while(sock.ouvert()) // While my socket is open
{

// To make messages still work:
while(::PeekMessage(&message,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}

// little break...
Sleep(300);

// send what i have to send in socket (with a socket->send(...)...)
vs->poster();

// Here I take care of messages i received (the OnReceive put message in a
list that is read here)
vs->lire();

}

return 0;
}



> joe
>


Thanks for advice :)

So what must I do ? :)


From: Joel on
>
> The CWinThread::Run function contains the message loop. If you are not
> calling it within your loop then you don't have a message loop and the
> socket will not receive its notifications.
>
> The MultiSoc sample in MSDN does what you are trying to do. Compare
> your code with that.
>
If you speaking about this article,
http://support.microsoft.com/default.aspx?scid=kb;en-us;175668

I did exactly the same, but there is not all the files described so I can't
see the Run() function or other stuff ...

Is there a full sample downloadable ?


Thanks.

Joel


From: Joel on
> _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
> ASSERT(pState->m_hSocketWindow != NULL); <---- HERE
>

I tried to understand ....

pState is OK, but m_hSocketWindows is null...

Why ?
I did my AfxSocketInit but it doesn t do a thing ...

I don't understand ...

Joel