Solved

How to process window messages during long operations.

Posted on 2001-08-10
6
243 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 150 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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