Windows Message Queue and Processor Performance

Posted on 2007-08-08
Last Modified: 2013-12-14
Hello all,

I'm new to windows programming, and there is something that really confuses me.

I am working on legacy code where the application logic is contained in various OnTimer calls in an MFC dialog based program that runs on a WinXP embeded system.

What appears to be happening is that the program spends "to long" in the timer event handler before responding to other messages like button clicks and such.  This causes a noticable delay in the system responsiveness when user interface features are used.

However, when I look at my processor usage in task manager, the processor is barely being utilized.  If there are events in my windows queue, and meaninful work to be done, why isn't the processor being used more?  The code that gets processed in the timer event handler does not use any Sleep, mutex, or file IO function, so why the holdup?

I am in the process of moving most of the application logic to different threads to free up the user interface thread, but it would be really helpful if, atleast temporarily, I could increase the CPU timeslice or some other "parameter" to improve processor usage for the application.  We use Microsoft's Target/component designer to build customized WinXP images, so maybe there's something in there I can change?

Question by:StixGP
    LVL 86

    Expert Comment

    >>The code that gets processed in the timer event handler does not use any
    >>Sleep, mutex, or file IO function, so why the holdup?

    So how exactly does the code look like?
    LVL 63

    Assisted Solution

    Not sure, but if onTimer is  being used, shouldn't calls to the OS to return to the main process or Giveup time be made if the process is waiting for stuff, and not actually doing anything.

    This is especially true for any wait loops.

    I hope this helps !

    Author Comment

    >>The code that gets processed in the timer event handler does not use any
    >>Sleep, mutex, or file IO function, so why the holdup?

    It appears I was wrong....big time...

    The section of code i've been referring to is tied to an ActiveX control sitting on the main dialog.  One of the object's member function (didnt see it at first) does a file I/O operation that takes on average 210 clock() ticks. That definetly explains the idle time and lag in the user interface.

    This operation is necessary to update the ActiveX control (MapObjects 2.2 by ESRI).  The code updates the maplayers recordset, and the map then refreshes on its timer message.  I don't think I can move the recordset update code to another thread because the mainthread is updating the map in real time and I doubt MapObjects is threadsafe.

    Any suggestions on how I can improve the UI responsiveness?

    LVL 63

    Expert Comment

    DO a release back to the OS immediately before and after the fileIO

    LVL 86

    Accepted Solution

    >>Any suggestions on how I can improve the UI responsiveness?

    Move that code into a thread and use

       MSG                 msg;

       HANDLE hThread = CreateThread ( ...);

       while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                               DispatchMessage     (   &msg);

    to wait for it to end. The loop will take care of dispatching messages and keeping the UI responsive.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
    Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    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.

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now