Solved

OnIdle processing for specific view in an MDI application

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 161
How to determine file rename from EventLog ? 2 41
Hibernate methods 2 67
changeXy challenge 13 67
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now