【发布时间】:2020-09-13 17:37:27
【问题描述】:
我正在编写一个 VSTO,它遍历所有幻灯片、所有形状并将标题设置为一个值。 我认识到每次运行后内存消耗都会增加。 因此,我最小化了我的代码并让它运行 100 次,最终为每 100 次运行分配大约 20MB 内存。
我的代码是从侧边栏按钮执行的,演示文稿有大约 30 张带标题的幻灯片。 我的按钮代码如下所示:
private void button1_Click(object sender, EventArgs e)
{
SetTitle_Direct();
Stopwatch watch = new Stopwatch();
watch.Start();
SetTitle_Direct();
watch.Stop();
//MessageBox.Show("Time spend: " + watch.Elapsed);
AMRefreshProgress.Maximum = 100;
AMRefreshProgress.Step = 1;
AMRefreshProgress.UseWaitCursor = true;
AMRefreshProgress.ForeColor = System.Drawing.ColorTranslator.FromHtml(ThisAddIn.amColor);
for (int i = 1; i <= 100; i++)
{
SetTitle_Direct();
AMRefreshProgress.PerformStep();
}
AMRefreshProgress.Value = 0;
AMRefreshProgress.UseWaitCursor = false;
Stopwatch watch2 = new Stopwatch();
watch2.Start();
SetTitle_Direct();
watch2.Stop();
MessageBox.Show("Time 1st run: " + watch.Elapsed + "\n Time 11th run: " + watch2.Elapsed);
}
SetTitle_Direct() 循环遍历幻灯片:
public void SetTitle_Direct()
{
PowerPoint.Presentation oPresentation = Globals.ThisAddIn.Application.ActivePresentation;
foreach (PowerPoint.Slide oSlide in oPresentation.Slides)
{
if (oSlide.Shapes.HasTitle == OFFICECORE.MsoTriState.msoTrue)
{
oSlide.Shapes.Title.TextFrame.TextRange.Text = "Test Main Title";
}
for (int iShape = 1; iShape <= oSlide.Shapes.Count; iShape++)
{
if (oSlide.Shapes[iShape].Type == Microsoft.Office.Core.MsoShapeType.msoPlaceholder)
{
if (oSlide.Shapes[iShape].PlaceholderFormat.Type == PowerPoint.PpPlaceholderType.ppPlaceholderSubtitle)
{
oSlide.Shapes[iShape].TextFrame.TextRange.Text = "Test Sub Title";
}
}
}
}
}
是什么导致插件分配越来越多的内存 - 或者如何避免这种情况?
【问题讨论】:
标签: memory vsto powerpoint