我有一个代码循环遍历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;
}
答案 0 :(得分:0)
许多控件的行为都是这样的:如果列表不可见,即使在简单的ListBox中添加项也会更快,这肯定是因为有些控件不可见时会停止处理与UI相关的所有消息(刷新,输入事件等)。
如果最小化窗口会给你带来显着的性能提升(你正在做足够的COM调用),我建议最小化窗口但用图像快照替换它,这样用户就不会真正看到窗口被最小化。您还可以覆盖微调器或进度条,让用户知道背后发生的事情。