p1
asked on
Thread with base class CFormView
How do I create a user-interface thread which has a base class of CFormView (instead of CWnd)?
User Interface Thread base class is CWinThread it is never a CWnd or CWnd derived Class
ASKER
My question is to implement a project which is MDI based format and
it will contain two different views. We want one thread to be setup for
each view.
Thus we want to use user interface thread because we need to
'postmessage' between the two windows.
Can you list the procedure needed to do this?
Or can you give me any example/source code?
Thanks
it will contain two different views. We want one thread to be setup for
each view.
Thus we want to use user interface thread because we need to
'postmessage' between the two windows.
Can you list the procedure needed to do this?
Or can you give me any example/source code?
Thanks
"Thus we want to use user interface thread because we need to
'postmessage' between the two windows. "
What would that really change? Simple threads can still post messages to other windows.
Could you clarify what the threads would be doing? Maybe a user-interface thread is too much for your use.
'postmessage' between the two windows. "
What would that really change? Simple threads can still post messages to other windows.
Could you clarify what the threads would be doing? Maybe a user-interface thread is too much for your use.
ASKER
Let me rephrase my original question again.
How do I create a USER-INTERFACE THREAD WINDOW which has a base class of CFormView (instead of CWnd). That is we like the user-interface thread to have its own window which is of base class CFormView.
I have difficulties in constructing it , and is asking for advices on how to do it.
Becasue I don't know how to combine the multithreading architecture and the CFormView construction code together - all I can do is to create a user-interface thread which has its own window of base class CWnd.
Once again, thank you for your time.
How do I create a USER-INTERFACE THREAD WINDOW which has a base class of CFormView (instead of CWnd). That is we like the user-interface thread to have its own window which is of base class CFormView.
I have difficulties in constructing it , and is asking for advices on how to do it.
Becasue I don't know how to combine the multithreading architecture and the CFormView construction code together - all I can do is to create a user-interface thread which has its own window of base class CWnd.
Once again, thank you for your time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Generate a Class derived from CWinThread
Look into online help to see what you need building an CWinThread derived class
Start the Thread using
AfxBeginThread(RUNTIME_CLA SS(CYourWi nThreadDer ivedClass) );
// have a look into online help for the parameters of AfxBeginThread
Now your Thread is running and AfxBeginThread or one of the functions that are called during the thread initialisation will call
InitInstance of the generated CYourWinThreadDerivedClass Object.
Inside the InitInstance function create a Frame window
and asign it to m_pMainWnd member variable of your CYourWinThreadDerivedClass Object and create the View:
BOOL CYourWinThreadDerivedClass ::InitInstance()
{
CCreateContext Context;
CMenu MyMenu;
CMainFrame* MyFrame=new CMainFrame;
m_pMainWnd=MyFrame;
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
MyMenu.LoadMenu(IDR_MAINFR AME);
if(MyFrame->GetMenu()) MyFrame->GetMenu()->Destro yMenu();
MyFrame->SetMenu(&MyMenu);
MyMenu.Detach();
Context.m_pNewViewClass=RU NTIME_CLAS S(CYourVie w);
CYourView* MyView=
(CYourView*)MyFrame->Creat eView(&Con text);
MyFrame->SetActiveView(MyV iew,TRUE);
MyView->ShowWindow(SW_SHOW );
ActivateFrame();
RecalcLayout(TRUE);
return true;
}
Look into online help to see what you need building an CWinThread derived class
Start the Thread using
AfxBeginThread(RUNTIME_CLA
// have a look into online help for the parameters of AfxBeginThread
Now your Thread is running and AfxBeginThread or one of the functions that are called during the thread initialisation will call
InitInstance of the generated CYourWinThreadDerivedClass
Inside the InitInstance function create a Frame window
and asign it to m_pMainWnd member variable of your CYourWinThreadDerivedClass
BOOL CYourWinThreadDerivedClass
{
CCreateContext Context;
CMenu MyMenu;
CMainFrame* MyFrame=new CMainFrame;
m_pMainWnd=MyFrame;
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
MyMenu.LoadMenu(IDR_MAINFR
if(MyFrame->GetMenu()) MyFrame->GetMenu()->Destro
MyFrame->SetMenu(&MyMenu);
MyMenu.Detach();
Context.m_pNewViewClass=RU
CYourView* MyView=
(CYourView*)MyFrame->Creat
MyFrame->SetActiveView(MyV
MyView->ShowWindow(SW_SHOW
ActivateFrame();
RecalcLayout(TRUE);
return true;
}
ASKER
Dear Norbert,
Sorry for replying late!!!
I have tried the method suggested by you but I found that the frame cannot be created.
I think this may due to the problem of the variable m_pMainWnd...each time
I execute my program, error occurs.
I used the debugger to locat the error, the error occurs at:
if (pParentWnd == NULL)
{
CWnd* pMainWnd = AfxGetThread()->m_pMainWnd ;
ASSERT(pMainWnd != NULL);
ASSERT_KINDOF(CMDIFrameWnd , pMainWnd); // error occurs at here
pParentWnd = (CMDIFrameWnd*)pMainWnd;
}
How can I correct this error?
Thanks
Sorry for replying late!!!
I have tried the method suggested by you but I found that the frame cannot be created.
I think this may due to the problem of the variable m_pMainWnd...each time
I execute my program, error occurs.
I used the debugger to locat the error, the error occurs at:
if (pParentWnd == NULL)
{
CWnd* pMainWnd = AfxGetThread()->m_pMainWnd
ASSERT(pMainWnd != NULL);
ASSERT_KINDOF(CMDIFrameWnd
pParentWnd = (CMDIFrameWnd*)pMainWnd;
}
How can I correct this error?
Thanks
if I found the right location the assertion happens inside the CMDIChildWnd::Create(...) function so I will ask some questions:
1.From wich thread do you call the Create function ?
AfxGetThread returns a pointer to the actual running thread. So if you call the Create function from outside the thread you get the wrong pointer.
2.Peece of my suggested code:
CMainFrame* MyFrame=new CMainFrame;
m_pMainWnd=MyFrame;
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
Is CMainFrame derived from CMDIFrameWnd
If you only want to create a child window of a CMDIFrameWnd inside the thread may be the following will work. But I am absolutely not sure about it because you are crossing threads and I do not know how thread safe the MFC is:
BOOL CYourWinThreadDerivedClass ::InitInstance()
{
CCreateContext Context;
CMenu MyMenu;
CYourMDIChildClass* MyFrame=new CYourMDIChildClass;
m_pMainWnd=AfxGetApp()-> m_pMainWnd ;
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
.
rest of InitInstance
1.From wich thread do you call the Create function ?
AfxGetThread returns a pointer to the actual running thread. So if you call the Create function from outside the thread you get the wrong pointer.
2.Peece of my suggested code:
CMainFrame* MyFrame=new CMainFrame;
m_pMainWnd=MyFrame;
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
Is CMainFrame derived from CMDIFrameWnd
If you only want to create a child window of a CMDIFrameWnd inside the thread may be the following will work. But I am absolutely not sure about it because you are crossing threads and I do not know how thread safe the MFC is:
BOOL CYourWinThreadDerivedClass
{
CCreateContext Context;
CMenu MyMenu;
CYourMDIChildClass* MyFrame=new CYourMDIChildClass;
m_pMainWnd=AfxGetApp()->
if (!MyFrame->Create(NULL, _T("Title Of your Window")))
return FALSE;
.
rest of InitInstance