每10秒运行一次功能

时间:2014-04-22 15:14:35

标签: c# winforms

我需要每10秒运行一次功能addFirstSlide,但我不知道如何以及在何处执行此操作。

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");
    }
 }

2 个答案:

答案 0 :(得分:3)

将一个计时器控件放到表单上,并将其.Interval属性设置为10000(1秒= 1000)。然后将您的代码放入计时器的Tick事件中。启用计时器后,Tick中的代码将每10秒运行一次。

答案 1 :(得分:1)

遵循:

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();
    }
}
}