Fog Creek Software
Discussion Board




what affects windows application responsiveness?

Not too familiar with how Windows works here, please bear with me.

I have a large application (an internal tool) that pretty much takes up the whole PC while it runs.  It appears to be CPU bound.  While it's running you can't really switch to anything else on the PC, it takes like 5 seconds to switch to your e-mail or something.

What factors affect this?  Is it simply because it is a cpu intensive program?  Where are the thread priorities set in Windows?  Can you just lower that?

Roose
Thursday, October 09, 2003

If developing in VB, remember to call DoEvents to allow other apps to run :-)

Take a look at threading, so you can run CPU-intensive parts of your program in asynchronous mode.

Frederic Faure
Thursday, October 09, 2003

All windows applications have a message queue and somewhere within the application there is a message loop that processes the messages from this queue.

The code that does this will be something similar to this:

  MSG msg;
 
  while (GetMessage(&msg, (HWND) NULL, 0, 0))  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

Now consider what happens if a menu item is selected.

Windows will put the menu message into the application message queue, it is retrieved by GetMessage and eventually it eventually gets to your menu handling code by the call to DispatchMessage.

Now consider what happens if the menu handling code takes a long time to complete. The code was called directly by DispatchMessage (in a round about sort of way) and this means until your menu handling code completes, the message loop is blocked.

As no more messages are being processed your application is effectively hung.

Jussi
Thursday, October 09, 2003

Is this running under NT/2000/XP or 95/98/Me? 

The NT branch tends to be pretty good about sharing the CPU among tasks.  You might see slow down with a CPU intensive task running (assuming Normal priority) but responsiveness should be pretty good.  Five seconds to switch to an email reader does not sound right.

Is the process memory intensive?  Using a large amount of memory tends to force Windows to swap the memory of inactive tasks out to disk.  When you switch to one of these tasks, Windows has to swap the memory back from disk.  Of course, the fact that a CPU/memory intensive task is running while it's trying to do this doesn't help.  This is the most common cause of slowdown like you described that I've seen.

The best tool for figuring out what's going on is Task Manager.  If your task is eating up a ton of memory, you can either live with it, reengineer it to use less memory, or buy more memory. 

Take a look at the Win32 API calls SetThreadPriority and SetPriorityClass for information on changing the process and thread priority. 

SomeBody
Thursday, October 09, 2003

Also try calling a function at regular intervals to empty your message queue, like at each iteration through the loop where you're doing your big calculations -- e.g.

void EmptyMessageQueue()
{
  MSG msg;
 
  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
  }

  return;
}

Rick
Thursday, October 09, 2003

Yes the app is using a ton of memory, which is a good point.  It can easily use up to 1/2 a gig, but typically our machines have a gig of RAM.

Thanks for those API calls, I will try those out...

Roose
Friday, October 10, 2003

It doesn't necessaril;y save you if you're using half a gig out of a gig. Windows will swap out other processes if it thinks it can better use the memory for other things, such as disk cache.

So if the other application is doing nothing in the background, windows might decide that it's a good time to swap it out. Once that happens, there's nothing you can do - you just have to accept the fact that it's going to take some time to swap it back in once you bring it to the foreground again.

It's a tradeoff the OS makes - although this causes a slowdown if an application that was inactive suddenly becomes active, 99% of the time, it's better use of resources to dedicate al available RAM to improve the performance of the currently running apps.

Sum Dum Gai
Friday, October 10, 2003

Assuming the app actually does some compute-intensive work, an option is to get a good profiler and use it to see where in your code the app spends its time.

I use them a lot (mainly Rational's) and almost every time the bottlekneck is not where intuition would suggest it will be.

Its humbling, but incredibly rewarding to see your app run 50% faster after modifying (typically) less than 10 lines of code....

anon
Friday, October 10, 2003

Wouldn't mention this on the forum if it hadn't already cropped up but there's one application I use "Broderbund Home Design 5.1" which is using up nearly 100% of CPU time, even when idle. As it uses 100+MB of memory, I presume the actvity is the OS swapping memory in and out of the swap file all the time. I have 384MB of RAM (the chipset won't take any more) which is enough for all running processes, even when this program is working, and am running W2K. Is there any way I can tweak settings to stop this behaviour.

Stephen Jones
Friday, October 10, 2003

Something to bear in mind that isn't really obvious. If you're running on a desktop-class OS (Windows 2000 Professional, either version of Windows XP), then priority is given to foreground (i.e., focused) applications. Their threads are priority-bumped. An app in the foreground that is constantly 100% CPU bound can easily make the machine seem laggy.

There is a solution here, even on the desktop class OSes: change the way Windows handles foreground apps. On Windows XP, right click My Computer, then properties. Go to the Advanced tab, and click "Settings" in the Performance box. On the Performance dialog, click the Advanced tab. The box labelled "Processor Scheduling", change "Adjust for best performance of:" to Background Services. This eliminates the foreground application thread bump.

Alternatively, you could programmatically change the priority of your main thread (or computing thread) downward.

Brad Wilson (dotnetguy.techieswithcats.com)
Friday, October 10, 2003

> While it's running you can't really switch to anything else on the PC, it takes like 5 seconds to switch to your e-mail or something.

This actually ties in with a few other threads at the moment. It's called incompetent programmer. You get em in Java. I guess you get em in Windows too.

Whoever developed the pile of crap probably did things that looked cute liked boosting the thread priority, capturing focus in violation of good Windows development practice, and probably using bloated components or frameworks meant for morons.

JM
Monday, October 13, 2003

*  Recent Topics

*  Fog Creek Home