Cross-thread issue of MFC csocket

Source: Internet
Author: User
VC: assert (pstate-> m_hsocketwindow! = NULL );

Csocket asserted error: assert (pstate-> m_hsocketwindow! = NULL );

Cause: When the socket is in the connection or sending status, it tries to close the socket, so it is interrupted at this asserted statement.

Cause analysis ::

Microsoft official explanation:

This assertion failure occurs because the csocket object was either created or accepted in the context of another thread. the socket Notification window was created in a different thread, and the m_hsocketwindow for the current thread is null, thus the assertion failure.

I understand that I use csocket across threads. The result is ....


As already mentioned, a casyncsocket object shoshould be used only in the context of a single thread. if you need to switch the thread that is accessing a socket connection with another thread, then you shoshould use a separate casyncsocket object in each thread, and use the detach and attach functions to attach the casyncsocket object to the socket handle in the thread that is about to use the socket. use this sequence:

Use detach () to detach the casyncsocket object from the socket handle in the thread that is currently using the casyncsocket object.

Use attach () to attach a different casyncsocket object to the socket handle while in the context of the mfc ui thread in which you wish to begin accessing the socket connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.