Child Windows Freeze on large SQL query

Posted on 2003-11-25
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;

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

class CMyRecView : public CRecordView

void CMyRecView::OnBnClickedRequest()

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


Many Thanks,
Question by:DBOTMA
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) )



Good LUck
LVL 39

Accepted Solution

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:

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
     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++)
            if (m_pRs->IsEOF())
                  delete m_pRs;
                  m_pRs = NULL;
            // do something with record

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

Author Comment

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?


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 125
iSeries FTP Exit Program 8 136
java ^ examples 8 67
How to convert MFC::CString to UTF8 wchar_t* 10 214
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

822 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