From: Joel on
Thank you, both of you.

I found !

Here it is:

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();


The problem is in this line : tacheEc->setSocket(s.Detach());
What does this function ?
....
sock.Attach()
....

So I called a function of my thread to do the Attach, but it s still my main
thread that execute the Attach. So I moved my Attach in the InitInstance of
my thread and all is perfect :)


Thank you so much

Byebye

Joel


From: Joseph M. Newcomer on
It says you have a bogus handle map. This can be caused by passing a CAsyncSocket poitner
across a thread boundary, instead of a raw socket and rebinding it. See my essay on the
use of UI threads on my MVP Tips site.

Putting an AfxMessageBox in a thread is begging for trouble. Seriously demanding the
system mess you over. Avoid this.

For more, see below...
On Wed, 2 Mar 2005 09:18:08 +0100, "Joel" <nospam(a)nospam.com> wrote:

>
>"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);
*****
COMPLETE AND TOTAL DISASTER! This is completely wrong. If you need to put a Sleep() in to
make a multithreaded system work, the design, by definition, is flawed beyond recovery. If
you don't have the Sleep(), you can use 100% of the CPU; if you do have it, you
artificially reduce the throughput to no more than 3 messages per second with this value.
In either case, the design is wrong.

The whole design doesn't make any sense. Throw everything away here. By doing
DispatchMessage you defeat the message table dispatching, which is also used to dispatch
socket messages!

Do the AfxSocketInit in the InitInstance handler, NOT in the Run() handler.

There is no way to "fix" this code. Discard it entirely.

Even the concept of asking "while the socket is opened" is a mistake. Who closes the
socket? If your thread closes the socket, it could do a PostQuitMessage to indicate it is
done. If some other thread can close the socket, this code is by definition incorrect,
since it will never, ever be able to work correctly.

There are good examples in the MSDN of using asynchronous sockets. Model your code on one
of those.

I have no idea what vs->poster() will be doing, but it is inappropriate in the Run()
routine. If you have a message to send via the UI thread, do a PostThreadMessage to the UI
thread to pass the buffer and other information in; this is tirival, and will be handled
automatically by the standard UI message handler; just have an ON_THREAD_MESSAGE or better
still ON_REGISTERED_THREAD_MESSAGE handler to initiate the send operation.
*****
>
>// 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 ? :)
>

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