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
Solved

OnIdle processing for specific view in an MDI application

Posted on 2006-10-29
4
393 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 95
bigDiff challenge 17 126
centeredAverage challenge 8 179
java ^ examples 8 69
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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