0

I need to run function addFirstSlide every 10 seconds, but I don't know how and where to do it.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void addFirstSlide()
    {
        PowerPoint.Slide firstSlide = Globals.ThisAddIn.Application.ActivePresentation.Slides[1];
        PowerPoint.Shape textBox2 = firstSlide.Shapes.AddTextbox(
        Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 50, 500, 500);
        textBox2.TextFrame.TextRange.InsertAfter("firstSlide");
    }
 }
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
user3357400
  • 397
  • 2
  • 9
  • 26
  • 5
    Use [Timer](http://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=vs.110).aspx) – Habib Apr 22 '14 at 15:15
  • Google: "run function every 10 seconds in C#" says: http://stackoverflow.com/questions/6169288/execute-specified-function-every-x-seconds – fast Apr 22 '14 at 15:15
  • You should solve with a Timer, read this: http://msdn.microsoft.com/es-es/library/system.timers.timer(v=vs.110).aspx and http://www.dotnetperls.com/timer – yosbel Apr 22 '14 at 15:16

2 Answers2

3

Drop a timer control on to the form and set it's .Interval property to 10000 (1 second = 1000). Then place your code in the timer's Tick event. Once you enable the timer the code in Tick will run every 10 seconds.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Good suggestion. Since `System.Windows.Forms.Timer` has no `AutoReset` property, I'd also use a `lock` block to prevent multiple instances from running. – Francis Ducharme Apr 22 '14 at 15:32
1

follow this :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void addFirstSlide()
    {
        PowerPoint.Slide firstSlide =  Globals.ThisAddIn.Application.ActivePresentation.Slides[1];
        PowerPoint.Shape textBox2 = firstSlide.Shapes.AddTextbox(
        Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 50, 500, 500);
        textBox2.TextFrame.TextRange.InsertAfter("firstSlide");
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Interval = 1000;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        addFirstSlide();
    }
}
}
Emoji
  • 34
  • 1