Click to See Complete Forum and Search --> : AfxBeginThread - Threads and Doc/View


May 3rd, 1999, 07:51 PM
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

macgowan@pobox.com

May 3rd, 1999, 08:31 PM
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.

BrianOG
May 4th, 1999, 09:16 AM
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

May 4th, 1999, 10:33 AM
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
macgowan@pobox.com