Solved

OnIdle processing for specific view in an MDI application

Posted on 2006-10-29
4
400 Views
Last Modified: 2013-11-20
I have an MDI application that has three views (see code excerpt below). Each view may be opened independently and are not automatically opened when the appplication starts. I want to do some background tasks on a specifc view only if the view has been activated and the message pump is idle. I know that I need to use the virtual OnIdle function from withing the CWinApp derived class.

My question is how do I access the proper view and call the appropriate background task. In this case the 'COrderEntryView'

BOOL COrderEntryApp::InitInstance() // partial code below...

                pOrderViewTemplate = new CMultiDocTemplate(
            IDR_ORDERETYPE,
            RUNTIME_CLASS(COrderEntryDoc),
            RUNTIME_CLASS(CChildFrame), // custom MDI child frame
            RUNTIME_CLASS(COrderEntryView));
      AddDocTemplate(pOrderViewTemplate);

      pLineSearchTemplate = new CMultiDocTemplate(
            IDR_LINEVIEWTYPE,
            RUNTIME_CLASS(CLineSearchDoc),
            RUNTIME_CLASS(CLineSearchFrame), // custom MDI child frame
            RUNTIME_CLASS(CLineSearchview));
      AddDocTemplate(pLineSearchTemplate);

      pReportViewTemplate = new CMultiDocTemplate(
            IDR_REPORTVIEWTYPE,
            RUNTIME_CLASS(CReportsDoc),
            RUNTIME_CLASS(CReportsFrame), // custom MDI child frame
            RUNTIME_CLASS(CReportsView));
      AddDocTemplate(pReportViewTemplate);

///////////////////////////////////////////////////////////////////////////////////////////////

BOOL COrderEntryApp::OnIdle(LONG lCount)
{

      if (CWinApp::OnIdle(lCount))
            return TRUE;  

               // If there are no messages in the pump I want to call a function within a particular view to do background processing
              //  For Example COrderEntryView::MyBackGroundTask()


               return TRUE;
}
0
Comment
Question by:carlmahon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17832101
Why not use a thread in the view?
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 350 total points
ID: 17832726
>>My question is how do I access the proper view and call the appropriate background task. In this case the 'COrderEntryView'

You can call  GetActiveView( ) from your CFramWnd class to get a pointer to your active view.  you have to cast your pointer from  GetActiveView( ) to your view class.

        CFrameWnd *pFrame = (CFrameWnd *) AfxGetMainWnd();
        COrderEntryView *pView = (COrderEntryView *) pFrame->GetActiveView();
   
-Otherwise you may use following function to get instance of 'Specific' view from your OnIdle function :

CWnd *FindClassView(CRuntimeClass *pViewClass)
{
    CWinApp* pApp = AfxGetApp();        
    CDocTemplate* pTemplate;
    POSITION pos = pApp->GetFirstDocTemplatePosition();
    while (pos != NULL)
    {
        pTemplate = pApp->GetNextDocTemplate(pos);
        ASSERT(pTemplate);

        POSITION pos2 = pTemplate->GetFirstDocPosition();
        while (pos2)
        {
            CDocument* pDoc = pTemplate->GetNextDoc(pos2);
            ASSERT(pDoc);

            POSITION pos3 = pDoc->GetFirstViewPosition();
            while (pos3 != NULL)
            {
                CView* pView = pDoc->GetNextView(pos3);
                ASSERT(pView);
                if (::IsWindow(pView->GetSafeHwnd()))
                {
                    if(pView->IsKindOf(pViewClass))
                    {
                        return pView;
                    }                    
                }
            }
        }
    }
    return NULL;
}


Use it like :---

BOOL COrderEntryApp::OnIdle(LONG lCount)
{

     if (CWinApp::OnIdle(lCount))
          return TRUE;  

    COrderEntryView *pView;
    pView=static_cast<COrderEntryView *>(FindClassView(RUNTIME_CLASS(COrderEntryView)));

   // Invoke function :
  if(pView)
    pView->MyFunction(...)
   
  //.......
 
  return TRUE;
}

-MAHESH
0
 
LVL 4

Author Comment

by:carlmahon
ID: 17839113
MAHESH - Thanks for the code! I knew what had to be done I was just unsure of the casting to get the specific view I needed. The code plugged in and worked perfectly. I will award all points to you.

Thanks for other responses, I had thought about the thread as well too, but since the questions was how to to the onIdle() I will not split points for other alternatives.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17840218
Fine by me - I mentioned it in case you either hadn't thought of it or just were afraid (too complex to code) of it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question