Solved

Child Windows Freeze on large SQL query

Posted on 2003-11-25
3
316 Views
Last Modified: 2013-11-20
I have a C++ MDI  application using MFC with SQLServer. The sql query is complex and takes approximately 20 sec to complete. This freezes the application and the user can't do any thing in the other child windows. I would like to know what should be done to allow the user to continue on the other Child windows while the sql query retrieves its information.

A snip of my code:

CDatabase m_db;


Interface::Interface()
{
      m_db.OpenEx("DSN=MYsql;Description=test;APP=Microsoft\x00ae Visual Studio .NET;WSID=TEST;DATABASE=myDB;Trusted_Connection=Yes");
}

CMyRecView.h
-----------------
class CMyRecView : public CRecordView
{
protected:
      CMyRecView();
..
..
}

CMyRecView.cpp
-----------------
void CMyRecView::OnBnClickedRequest()
{


      CDynamicBulkSet rs(&m_db);
      rs.Open(CRecordset::snapshot, "SELECT * FROM tblLeaders", CRecordset::readOnly | CRecordset::useMultiRowFetch);

      FillGrid(&rs);
      rs.Close();
}


Many Thanks,
Dan
0
Comment
Question by:DBOTMA
[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
3 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9822399
I think the problem is not in the query, the function FillGrid will take much amount of time for a bulk data, so the messages will block add this snippet in that function (in the loop of that function)

void CYourClass::FillGrid(CRecordset& rs)
{
      MSG msg;

      (******* LOOP *******)
      {

            while ( ::PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE) )
            {
                  ::TranslateMessage(&msg);
                  ::DispatchMessage(&msg);
            }

            m_wndYourList.InsertItem(.........);

      }



Good LUck
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 9826343
The problem is, that you call the lengthy job when the BUTTON_CLICKED message is processed.
So, no other messages could be processed as long as are in CMyRecView::OnBnClickedRequest()

Rosh showed you how to process messages within your handler function, thus avoiding the problem.

Another way is to handle only a small portion of your result set within one message handling.
You would setup a timer, that is called say all 10 ms and that will fill say 10 records to your grid.
This would look like this:
 

CMyRecView.cpp
-----------------
void CMyRecView::OnBnClickedRequest()
{
     // You need the CDynamicBulkSet as member
     // CDynamicBulkSet rs(&m_db);

     if (m_pRs != NULL)
     {
         // do not handle button clicks while an old result set exists
          return;
     }
     m_pRs = new CDynamicBulkSet (&m_db);
     m_pRs->Open(CRecordset::snapshot, "SELECT * FROM tblLeaders", CRecordset::readOnly | CRecordset::useMultiRowFetch);

     // setup the timer
     m_timerID  = SetTimer(123, 10);

     // FillGrid(&rs);
     // rs.Close();
}

void CMyRecView::OnTimer(UINT eventId)
{
      for (int i = 0; i < 10; i++)
      {
            m_pRs->MoveNext();
            if (m_pRs->IsEOF())
            {
                  KillTimer(m_timerID);
                  m_pRs->Close();
                  delete m_pRs;
                  m_pRs = NULL;
                  return;
            }
           
            // do something with record
            FillNextGridLine(m_pRs);
            ...
      }
     
}

I hope, you got the idea.

A further possibility would be to do the whole job in a thread.
I can give you the details if you want them.

Regards, Alex
0
 

Author Comment

by:DBOTMA
ID: 9863639
The problem is not with FillGrid, even with the line commented out it still take 20 seconds.

Please note: The sql "select * from tblLeaders" is only to show the problem.

Any better solutions than using a worker thread?

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
sumNumber challenge 16 147
java ^ examples 8 75
twoTwo  challenge 35 119
mapAB Challlenge 35 185
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

734 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