I'm writting a VSTO which loops through all slides, through all shapes and sets the title to a value. I recognized that the memory consuption is going up after each run. So therefore I minimized my code and let it run a 100 time which ends up allocating about 20MB Memory for every 100 runs.
My code is executed from a sidebar-button, the presentation has about 30 slides with titles. My code looks like this for the button:
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);
}
The SetTitle_Direct() loops through the slides:
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";
}
}
}
}
}
What causes the AddIn to allocate more and more memory - or how could this be avoided?