1

I have a code which loops over all the tables in a word document and reads the width values on every cell. For some reason, I've noticed that this code runs about 5-10x faster when the window is minimized. To get an idea, when I run it with the window maximized, it takes ~0.02 seconds to make the Cell.Width call, but with the window minimized, it takes .001 to .0015 seconds per call.

Anyone know what could be causing this, and if I could reproduce these results without minimizing the window. As a user, it would be very weird to see your window minimize/maximize randomly to do operations.

    //doc and app are the active Document and Application respectively. 
    try 
    {
        app.ScreenUpdating = false;
        //app.WindowState = word.WdWindowState.wdWindowStateMinimize; //enabling this improves speeds by 5x to 10x
        foreach (word.Table table in doc.Tables)
        {
            //loop over every cell in a table and read/store its cell.Width value.
        }
    }
    finally
    {
        //you can alternatively store the original values here and restore them to that. For simplicity, I did not do that here.
        app.ScreenUpdating = true;
        app.WindowState = word.WdWindowState.wdWindowStateMaximize; 
    }
3
  • I noticed this behavior in Excel, and while this isn't a definitive answer, I assumed that there must be code that runs only when the UI is visible - code that does double-buffering, for example, to update the display. My guess was an is that Microsoft coded it this way to increase performance. SIt makes logical sense to skip the UI cycles when it doesn't matter. However, unless someone from the Office development team sees this and posts an answer, I'm betting that there won't be a definitive answer on this.
    – David
    Mar 26, 2012 at 20:01
  • I have always noticed this in Word also. I keep the application minimized on purpose while updating MergeFields. If you keep it open every update moves the document to show where I change the text. Of course this slow down the process.
    – Steve
    Mar 26, 2012 at 20:04
  • I thought that setting screenUpdating=false would take care of the buffering that needs to happen in the UI. I suppose there is something else that happens that we have no control over? It also seems unusual that a "Read" operation would be causing this effect. I could understand it more if it was a write operation.
    – Shark
    Mar 26, 2012 at 20:07

1 Answer 1

0

A lot of controls behave like this: even adding items in a simple ListBox is faster if the list is not visible, and this is definetly because some when a control is not visible it will stop processing all messages that are UI related (refresh, input events, etc).

If minimizing the window is giving you a significant performance boost (you are doing enough COM calls), I would suggest to minimize the window but replace it with an image snapshot, so the user doesn't really see the window getting minimized. You can also overlay a spinner or progress bar to let the user know something is happening behind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.