CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    AfxBeginThread - Threads and Doc/View

    I am using the Document View Architecture and starting a Thread using AfxBeginThread(WorkerThread, pParam);
    When I am in the WorkerThread how can I access a method in my View Class. I have tryed the following:

    CMainFrame *pMainFrame = (CMainFrame *)AfxGetMainWnd();
    CFrameWnd* pChild = pMainFrame->GetActiveFrame();
    CServerView* pView = (CServerView*)pChild->GetActiveView();

    // The method I am tryitng call from the Thread
    pView->WriteLogMessage(szMessage);

    This will fail with a Access Violation on the call to GetActiveFrame()

    I have also tryed throught about used CWnd::PostMessage();

    Any comments would be appreciated!
    Thanks,
    Chris Macgowan

    [email protected]



  2. #2
    Guest

    Re: AfxBeginThread - Threads and Doc/View

    Try to use the pView pointer as an argument pParam to AfxBeginThread call.
    From worker threads (that is, other than main threads), AfxGetMainWnd may fail.




  3. #3
    Join Date
    May 1999
    Posts
    116

    Re: AfxBeginThread - Threads and Doc/View

    I have achieved something like this by passing the hWnd if the View in the pParam.
    Then in the worker thread I use
    ::SendMessage(hWnd, WM_XXXXXXX, ..., ...);
    Where WM_XXXXX is a custom defined message.
    Note: if you use PostMessage you must ensure that the message is processed before the string you are using goes out of scope


  4. #4
    Guest

    Re: AfxBeginThread - Threads and Doc/View

    Thank You Anonymous - Passing in the pointer to the View worked fine -
    Here is the code if anyone is interested:

    This is the call using AfxBeginThread()
    ------------------------------------------------------------
    CMainFrame *pMainFrame = (CMainFrame *)AfxGetMainWnd();
    CFrameWnd* pChild = pMainFrame->GetActiveFrame();
    CHTTPServerView* pView = (CHTTPServerView*)pChild->GetActiveView();
    AfxBeginThread(ServerThreadProc, pView);


    This is the ServerThread()
    -----------------------------------
    CHTTPServerView* pView = (CHTTPServerView*)pParam;
    // print the first header
    pView->WriteLogMessage("We are indide the thread: ServerThreadProc()");

    Thanks,
    Chris Macgowan
    [email protected]



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured