|
-
May 3rd, 1999, 07:51 PM
#1
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]
-
May 3rd, 1999, 08:31 PM
#2
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.
-
May 4th, 1999, 09:16 AM
#3
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
-
May 4th, 1999, 10:33 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|