Re: Losing the Document in a SDI application

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Neel Roy (neelagain_at_hotmail.com)
Date: 05/04/04


Date: Wed, 5 May 2004 03:16:50 +0530

Thoughts and hints...

> CMyMainFrame::GetActiveDocument();

this aint possible... so you must be using either inside MainFrame class or
getting a pointer...

1)
If getting a pointer perhaps this will work... ONLY if you are calling it
from primary thread...

CDocument *pDoc = ((CMainFrame*)AfxGetMainWnd())->GetActiveDocument();

2)
If calling from worker thread, you need to get App pointer first... because
of implementation of AfxGetThread() and then pApp->m_pMainWnd

_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
 { CWinThread* pThread = AfxGetThread();
  return pThread != NULL ? pThread->GetMainWnd() : NULL; }

3)
Since GetActiveDocument() is virtual you can set view you want to be active
and then get the document of that view... perhaps no view is active at that
moment...

CDocument* CFrameWnd::GetActiveDocument()
{
 ASSERT_VALID(this);
 CView* pView = GetActiveView();
 if (pView != NULL)
  return pView->GetDocument();
 return NULL;
}

Article which might be helpful :

INFO: Switching Views in a Single Document Interface Program

Q99562

"Phillip N Rounds" <prounds@cassandragroup.com> wrote in message
news:OgeS0qeMEHA.556@TK2MSFTNGP10.phx.gbl...
> I have a fairly complicated SDI application which recently has had the
> problem that it, at times, looses the document.
> Basic architecture is
> SDI, with a splitter window.
> The Document has three views:
> View1 is always present in Pane(0,0) of the splitter.
> View2 and View3 are presented in Pane(0,1) of the splitter, with the
> Mainframe handling the switching between the two.
>
> The application has a serial connection to a piece of lab equipment. We
> have a threaded serial communication class, which posts a message when
data
> is received. When the RECEIVED message is posted, the Mainframe reads the
> data and stores it in an array for later processing.
> The Mainframe invokes WM_TIMER, with two timers: One at one second to
> refresh the views with current data, the second at (variably) specified
> intervals to process the data. Data gets initial processing in the
> Mainframe, then put into the Doc.
>
> The two views are as follows: View1 is a CView, which is used to present a
> graphical representation of the data. View2 is a CListView
> to present the data in tabular form. Printing is handled within the
views,
> in View1 by passing a CPrinterDC to the OnDraw() method.
> View2 prints with formatted printing of the data to the printer.
>
> All of this is probably irrelevant, but what the hell.
>
> The problem is that the app will hang at random times. In debugging this,
> the problem is always that the Mainframe loses the document.
>
> CMyDocument* pDoc = CMyMainFrame::GetActiveDocument();
> results in pDoc = NULL
>
> I have added the m_bAutoDelete = false to the CMyDocument constructor,
and
> it made no difference.
> I put a trace into the CMyDocument destructor to see when that was being
> called, and it never was.
>
> Any guesses as to how this may be happening?
>
> Thanks
>
> --
> Phillip N Rounds
> The Cassandra Group, Inc
> 68 Sand Hill Rd
> Flemington NJ 08822
> prounds@cassandragroup.com
>
>



Relevant Pages

  • Re: Losing the Document in a SDI application
    ... If getting a pointer perhaps this will work... ... of implementation of AfxGetThread() and then pApp->m_pMainWnd ... > Mainframe handling the switching between the two. ...
    (microsoft.public.vc.language)
  • Re: Losing the Document in a SDI application
    ... If getting a pointer perhaps this will work... ... of implementation of AfxGetThread() and then pApp->m_pMainWnd ... > Mainframe handling the switching between the two. ...
    (microsoft.public.vc.mfc)
  • Re: AfxGetApp()->GetMainWnd()->SendMessage() to MainFrame not working.
    ... You should be able to tell if the Pointer you get from AfxGetMainWnd is ... Set a break point in one of the CFrameWindows create ... > Something is failing with sending messages to the MainFrame. ...
    (microsoft.public.vc.mfc)
  • Re: How to pass data from mainframe to modal dialog?
    ... Create a pointer to this variable in the dialog, and set it before calling the dialog. ... example below presumes that the dialog is being called from the mainframe. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: How to pass data from mainframe to modal dialog?
    ... Or better (provided that the appropriate constructor is defined): ... > Create a pointer to this variable in the dialog, and set it before calling ... > example below presumes that the dialog is being called from the mainframe. ... > will have to get access to the mainframe to get the pointer. ...
    (microsoft.public.vc.mfc)