?
Solved

How to process window messages during long operations.

Posted on 2001-08-10
6
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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