How to create new document with view from the thread?
Hi!
I have one problem:
I can't create view of the document from the thread...
I do like this:
==================================
pDoc = MyDocTemplate->CreateNewDocument();
pDoc->OnNewDocument();
CFrameWnd *pFrame = NULL;
try
{
pFrame=MyDocTemplate->CreateNewFrame(pDoc,0);
}
catch (...)
{
AfxMessageBox("couldn't create new frame");
}
if(pFrame==NULL)
{
AfxMessageBox("couldn't create new frame");
}
MyDocTemplate->InitialUpdateFrame(pFrame,pDoc);
=============================================
If I call this code from application class, than all is ok!
But problems started, if I will call this function from the thread. Document will be created, but CreateNewFrame doesn't work, it makes Exception ;(.
What can I do to avoid this problem?
Thank you in advance...
[928 byte] By [
kansoft] at [2007-11-18 2:14:35]

# 1 Re: How to create new document with view from the thread?
You did not provide enough information to answer your post.
What kind of thread?
What exactly error message says and what line of code is causing it?
Besides by calling CreateNewDocument member of doc template you already requested new frame and view as registered with template. Do you need another frame?
JohnCz at 2007-11-10 8:56:35 >

# 2 Re: How to create new document with view from the thread?
You might be running into problems with the temporary and permanent maps for windows handles. Read this MSDN article entitled Multithreading: Programming Tips (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Multithreading.3a_.Programming_Tips.asp)
In general, you can't access CWnd objects across threads, and your CFrameWnd object is derived from CWnd.
You might try an entirely different approach, and post a user-defined message from your secondary thread to the main thread. (Make sure you do a PostMessage and not a SendMessage.) The message would signify to the main thread that it (and not the secondary thread) needs to create a new doc. THis avoids the problem of accessing CWnd objects across threads. You can set the lParam of the message to whatever you need to get the doc up and runnign (e.g., a pointer to a structure on the heap that holds your doc parameters).