Thread: MFC Multi-threading is working ... Now another problem :(

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Unhappy MFC Multi-threading is working ... Now another problem :(

    Ok, well, I got the MFC multi-threading working. I found some code on CodeProject.com. Now I have another problem. I defined my Listen() and Server() functions in my application's dialog class, and I had to make them 'static UINT Function()', etc. Now, when I use "AfxSocketInit(NULL)", where the parameter is "WSADATA*", it causes the problem. First, I used some code from CodeProject. It looks like this:

    Code:
    typedef struct 
    {
        CMyApplicationDlg *_this;   // Class's 'this' pointer
    } THREADSTRUCT;
    I have to use that so that I am able to access functions in the class from my 'static' member functions, since I can't directly access non-static class members from a static class member. When I do something similar to this:

    Code:
    // 'lpParam' is the parameter passed in "AfxBeginThread"
    THREADSTRUCT *ts = (THREADSTRUCT*) lpParam;
    
    // This causes the problem
    ts->_this->m_csListen.Accept(m_csClient);
    It gives me an error, and when I do "Debug", I get this line, from sockcore.cpp (I think that's the filename):

    Code:
    ASSERT(pState->m_hSocketWindow != NULL);
    I think it's because my thread functions (Listen() and Server()) cannot access the dialog class so they can use the dialog window as the socket window. If you can offer any help I would really appreciate it.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looking at the MFC code, "pState" comes from thread-local-storage. That means it is only valid in the thread that created the CAsyncSocket.

    Try accepting the connection in the thread that created "m_csListen", then kick off a new thread to handle the "m_csClient" connection.

    gg

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question I think I already did that ... ?

    Well, I create the listening (m_csListen) socket when the user clicks the "Start" button in my application. Then, it starts the "Listen()" thread. Inside the listen thread is where it calls "Accept()". You may be trying to tell me something else, but this is how I understand it.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Create m_csListen and call m_csListen.Accept() in the same thread.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC Message Map Problem
    By durban in forum C++ Programming
    Replies: 10
    Last Post: 10-31-2005, 01:55 PM
  2. PowerPoint and OLE Server and MFC problem
    By gicio in forum Windows Programming
    Replies: 0
    Last Post: 08-21-2003, 02:46 AM
  3. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  4. Major Multithread Problem :: MFC
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-07-2002, 09:58 PM
  5. Interesting Multithreading Problem :: MFC
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 04-22-2002, 02:28 PM