Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating GDI/Multithreading

Posted on 1998-06-19
14
Medium Priority
?
267 Views
Last Modified: 2010-04-10
First, apologies for this not-so-clear description of my problem...


I'm trying to develop an SDI application (App A) using MFC, VC 5++ Enterprise version. App A monitors how long a totally separate calculation program (App B) has been running.

Several timers indicate elapsed time, the estimated time when App B should finish etc etc.

Whilst App A is running, as soon as I activate another window that covers part of App A's window, that region of App A's window is wiped out, even though in my code every five seconds all the timer displays are updated using UpdateWindow().

Activating App A's window has no effect. It's not that part of the window doesn't update - it's that the _whole_ window doesn't redraw, even though it becomes active.

As soon as App B finishes, App A's timers stop as intended...and at last App A's window updates and fills the screen again.

In other words, in order to see an up-to-date timer output, I cannot activate any other window whilst App A is running, because as soon as I do, App A doesn't redraw until it's finished timing.


My question - does anyone out there know of a simpler way than using multi-threading to update/redraw the whole window whilst the timing is going on ?

Alternatively (the best solution), could someone please let me know where I can find a real "bare bones" MFC template for multi-threading. I took a look at the MS  multi-threading sample program and found it way too tricky to try and implement in my own code.

The GDI stays looking up-to-date whilst the main code handles the timers - is this really so difficult to implement or am I missing the obvious ?

I'm pretty new to Experts Exchange, so if you need more points, just say and I'll buy/try and save up some  more!

      Thanks a lot,

            Sexton
0
Comment
Question by:platopus
[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
  • 5
  • 3
14 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1166292
In order for your windows to be updated (repainted), you must be handling messages in a message loop.)  Does your code execute a message loop?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166293
How is Application A monitoring the time?  What you should do is set up 1 (or a few timers) for it and it should execute a normal windows message loop.  It should not be in a wait loop of some sort that does not process windows messages (at least not for a long time).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166294
It might help if you were to post some of your relevant code.  Give us an idea how application A is waiting, timing, handling messages, painting.
0
Independent Software Vendors: 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 32

Accepted Solution

by:
jhance earned 560 total points
ID: 1166295
From your description, you are running in a loop in App A waiting for App B to complete.  While App A is in it's loop, no window updates in App A can happen.  The problem is that these happen only when App A returns to Windows and Windows see that App A needs to be redrawn.  To solve this problem, you App A should setup a timer and then only check on App B's progress when the timer fires and a WM_TIMER (or OnTimer in MFC) message is received.  If App B is not done, then App A returns.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1166296
Oh, forgot to mention that "polling" your App B to see if it's don't is not the best way to do it.  Windows provides other notification methods that don't require a timer or periodic checking.  Probably the easiest is the WaitForSingleObject() API call.  This will allow you to start App B as a thread, set an expiration time, and then just wait.  If you want your windows to remain responsive (i.e. repaint itself), you need to start your App B as a thread, start another thread and call WaitForSingleObject() and have that thread send a message to your main thread when it completes using PostMessage().  In this case, you need no timers at all, Windows will handle the time-out period for you.  It's also more efficient as your app doesn't waste time responding to timer messages.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166297
jhance, how is your answer different than mine?  I was waiting to see his code before I answered, in case he was doing it correctly and I had guessed at wrong problem.
0
 

Author Comment

by:platopus
ID: 1166298
Hi Nietod,

This is my first C++ app of this scale, and I've only written apps using the MFC, so things are pretty primitive as far as message loops and suchlike !


Basically App A executes App B using ShellExecute( ).App A then performs the following loop (simplified a fair bit here to cut down on the length):

While (FoundAppBWindow ( ) == TRUE)
{    
     //code to increment all timer displays by one second.

    //now wait for 1 second
     ProcessSleep( (clock_t)1 * CLOCKS_PER_SEC );

     //if 5 seconds have elapsed then UpdateWindow( ).
     
}

void ProcessSleep(clock_t wait)
{
         clock_t goal;
         goal = wait + clock( );
         while( goal > clock( ) );
}


Thanks,
        Sexton


0
 
LVL 32

Expert Comment

by:jhance
ID: 1166299
Yes, your problem is that you are POLLING the clock instead of waiting for a notification message.  USe a windows TIMER and respond to the WM_TIMER message to know when it's timed out.  Your code never returns to Windows and even if you call UpdateWindow, the refresh doesn't get called, it just gets scheduled.  You must return from your application before Windows will refresh your window.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1166300
nietod,

Sorry to step on you, but I thought you were beating around the question.  This is a classic Windows programming mistake and having struggled through this myself, I was sure of the problem.  As you can see from his code, that IS the problem.

If it's not, I'm sure platopus will let us know.
0
 

Author Comment

by:platopus
ID: 1166301
Thanks for the comment jhance. Your analysis of the problem makes sense. I don't have any experience using these windows timers - if it's not too complex please could you provide me with a short code snippet showing how to use these ?

The reason I say this is that I keep coming across "new 32 bit" or "convenient" functions in VC++ only to find out that it takes 10 parameters and half a day to get them working in a "real life" program  !

I'm using ShellExecute instead of CreateProcess to start App B because I'm "running" an associated file (the equivalent of double-clicking a .doc file in Explorer to open it in Word), not an executable. The executable doesn't accept command line parameters. Can you perform this associated run action using CreateProcess ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166302
Agreed, That is why I said 1.  He must be handling messages in a message loop and not waiting in a wait loop and 2. he needs to create a timer (or two) to get notifications.

The same points you made.

but go for it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166303
That last comment was to jhance, not to platopus.

Just to keep you going platopus, why don't you look at the SetTimer()  API.  That is what jhance will show you how to use to create your timer notifications.  
0
 
LVL 32

Expert Comment

by:jhance
ID: 1166304
Here's how to setup a timer notfication using MFC:

1) Using the VC++ Class Wizard, add a WM_TIMER handler to your application.  You should see an OnTimer function added to your class.

2) Edit the OnTimer function to do your housekeeping when it is called and then return.

3) In your code, where you want to start the timer, add a SetTimer(1, 1000, NULL);  // The 1 is the timer ID and must be unique for each timer used in your application.  If you only have 1, then use 1.  The 1000 is the timer interval in mS.  So this is a 1 second timer.

4) When you are done with the timer or you app is exiting, call KillTimer(1);  Where the 1 is the ID of the timer used in SetTimer.
0
 

Author Comment

by:platopus
ID: 1166305
Thanks a lot to both of you for your comments. I got the SetTimer function working over the weekend.

The points go to jhance as he was the first to answer the problem correctly...sorry Nietod that you lost out through being cautious in diagnosing the problem...I'll be posting some real no-brainers in future so I'm sure you'll pick up some points on those :)

Sexton
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

660 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