Solved

How to process window messages during long operations.

Posted on 2001-08-10
6
240 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 150
Detect CR LF to each line 12 137
sumHeights  challenge 17 61
firstChar challenge 13 86
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

10 Experts available now in Live!

Get 1:1 Help Now