Solved

Child Windows Freeze on large SQL query

Posted on 2003-11-25
3
315 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
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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: 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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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