Re: Sendmessage: from one thread to mainthread (CFormView)
On 5 Sep., 23:09, Joseph M. Newcomer <newco...@flounder.com> wrote:
See below...
On Sun, 5 Sep 2010 11:01:36 -0700 (PDT), mfc <mfcp...@googlemail.com> wro=
te:
Hi,
I`m new to MFC; therefore I want to know if I do the right thing on
the right place :-). My mfc project is a unicode sdi application.
In my cdoc OnNewDocument class I load a xml file including all the
user-specific data. I hope this would be the right place to do that.
All xml nodes are stored in a cstringlist in the cdoc class.
BOOL CMIAppDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// Initialize COM
::CoInitialize(NULL);
//CStringList *m_XmlList; declaration in the cdoc header file
m_XmlList = XmlFile.LoadUserXml();
return TRUE;
}
To get this CStringList for each view - simple call
OnUpdateAllViews().
But which would be a suitable / recommend way to get the CStringList
m_XmlList to a non-mfc class?
****
Pretty much as you have already done. There's nothing there that is un=
ique to the
CDocument-derived class.
****
For example, I`ve installed a webserver
with a class HttpServer, which loads the specific html resource and
adds these information (inlcuded in m_XmlList) to the webpage.
class HttpServer
{
public:
HttpServer(void);
~HttpServer(void);
//and so on
}
BOOL HttpServer::GetFileRequest()
{
CString protoHTML("");
CString userHTML("");
//load html resource
if (GetHTML(id , protoHTML) == true)
****
Comparing this to 'true' is both dangerous and unnecessary. Get rid of=
the == true in the
above line!
****
Could you tell me why this line is dangerous? GetHTML is a function
which loads the html resource to protoHTML. If anything goes wrong
within this function -> FALSE will be returned instead of TRUE.
//get xml file resource from CFormView
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd) ;
CView * pView = pFrame->GetActiveView() ;
CStringList *plist = (CStringList *)pView-
SendMessage(UWM_GET_WEB_XML, 0, 0);
****
No, this is potentially dangerous, if the view is managed by another thre=
ad. You seem to
imply that it is. In this case, it would be the view's responsibility =
to have told this
thread about the XML data.
****
Thanks for your explanation; I`ll send a PostThreadMessage to the
specific view (the only suitable way to get the correct handle or
threadID will be properly to get it if this thread will be created).
The view method will send a Postthreadmessage back to another function
(HttpServer::GetFileFromView) and from here I can send the http
message.
Do you know if there`s a good solution, if the Postthreadmessage from
the view back to the HttpServer class will not be generated -> so that
I can send a "internal-server-error" or anyhting else back to the
user? Maybe starting a timeout after sending the Postthreadmessage to
the view?
best regards
Hans