?
Solved

How to process window messages during long operations.

Posted on 2001-08-10
6
Medium Priority
?
253 Views
Last Modified: 2013-11-20
I have an app which recursively searches directories for arbitrary file types. During this extensive search, the app will no longer respond. I would like to be able to continue processing window messages while the process is executing (executes within a child window). How can I accomplish this? Does this require spawning another thread for the search process?
0
Comment
Question by:sgraves66
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6373967
Any operation that takes a long time to run will block your app from processing Windows messages.  When this happens, the app will be unresponsive.

To work around this, start your search in a thread of its own.  This is exactly the kind of situation where threads make sense.
0
 
LVL 86

Accepted Solution

by:
jkr earned 450 total points
ID: 6374019
>>I would like to be able to continue processing window
>>messages while the process is executing (executes within
>>a child window).
>>Does this require spawning another thread for the search
>>process?

Spawning another thread would not be a too bad idea. But, simply 'draining the message queue from time to time during a lengthy operation is not too difficult:

void DrainMessageQueue()
{
  MSG msg;

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

So, when you're e.g. running through a loop, you could just

while ( whatever)
{
  DrainMessageQueue();

  // do processing
}

This will ensure that your GUI doesn't 'freeze'
0
 
LVL 1

Author Comment

by:sgraves66
ID: 6374149
Both answers are correct. I'll give the points to whomever can answer this, since it started after I created a worker thread. I have a CDialog-derived class which I use to display the progress of the search. Now, the app processes all messages. I need the dialog to be a child of the window running the process, the dialog shouldn't move,  and it shouldn't block any other child windows within the MDI main window. The child window running the process should not respond to any window message while the search is still active.

Increasing points.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 6374304
You could do that using 'AttachThreadInput()':

Remarks
Windows created in different threads typically process input independently of each other. That is, they have their own input states (focus, active, capture windows, key state, queue status, and so on), and they are not synchronized with the input processing of other threads. By using the AttachThreadInput function, a thread can attach its input processing to another thread. This also allows threads to share their input states, so they can call the SetFocus function to set the keyboard focus to a window of a different thread. This also allows threads to get key-state information. These capabilities are not generally possible.

0
 
LVL 1

Author Comment

by:sgraves66
ID: 6374365
Ok. I'm using a doc/view MDI setup. I've created a CWinThread-derived class and added some extra functionality. Where would I instantiate the thread class in order to have it manage the view associated with the document?
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6379089
sgraves:

a) "should not respond to any window message"
this implies that it isn't repainterd correctly. Is this what you want?

b) Keep all access to CDocument / CView in the main thread. The Doc/View architecture is not suited for multithreading.

MFC can handle "normal" window/dialog multithreading. but as soon as a CDocument gets involved, you're out for trouble.

Do all UI updates in the main thread. Design a memory  area where the threads exchange data. Use PostMessage from the worker to the main thread to inform the main thread it should update the UI.

To keep the dialog fixed at size & position (I wonder if I would like that), just override the OnSize/OnMove handler, and return immediately while search is active.

What do you mean with "should not block aby window of the main thread"  ??
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

864 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