Solved

Child Windows Freeze on large SQL query

Posted on 2003-11-25
3
312 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 87
string initialization in java 11 103
isEverywhere  challenge 19 62
MaxSpan challenge 9 65
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

15 Experts available now in Live!

Get 1:1 Help Now