最小化Word窗口可提高COM速度

本文关键字:COM 速度 Word 窗口 最小化 | 更新日期: 2023-09-27 18:29:07

我有一个代码,它在word文档中的所有表上循环,并读取每个单元格的宽度值。出于某种原因,我注意到当窗口最小化时,此代码的运行速度大约快5-10倍。为了得到一个想法,当我运行它时,with the window maximized, it takes ~0.02 seconds to make the Cell.Width call,但with the window minimized, it takes .001 to .0015 seconds per call

任何人都知道是什么导致了这种情况,如果我能在不最小化窗口的情况下重现这些结果。作为一个用户,看到你的窗口随机最小化/最大化来进行操作会非常奇怪。

    //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; 
    }

最小化Word窗口可提高COM速度

很多控件的行为都是这样的:如果列表不可见,即使在简单的ListBox中添加项也会更快,这是有定义的,因为有些控件不可见时,它会停止处理所有与UI相关的消息(刷新、输入事件等)。

如果最小化窗口可以显著提高性能(您正在进行足够的COM调用),我建议最小化窗口,但用图像快照替换它,这样用户就不会真正看到窗口被最小化。您还可以覆盖一个微调器或进度条,让用户知道后面发生了什么。