C# 每10秒运行一次函数

C# 每10秒运行一次函数,c#,winforms,C#,Winforms,我需要每10秒运行一次函数addFirstSlide,但我不知道如何以及在哪里执行 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void addFirstSlide() { PowerPoint.Slide firstSlide = Globals.ThisAddIn.Application.Ac

我需要每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");
    }
 }

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

在窗体上放置一个计时器控件,并将其
.Interval
属性设置为10000(1秒=1000)。然后将代码放入计时器的
勾选事件中。启用计时器后,滴答声中的代码将每10秒运行一次。

遵循以下步骤:

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();
    }
}
}
遵循以下步骤:

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

使用谷歌:“在C#中每10秒运行一次函数”说:你应该用定时器来解决问题,读这个:使用谷歌:“在C#中每10秒运行一次函数”说:你应该用定时器来解决问题,读这个:还有很好的建议。由于
System.Windows.Forms.Timer
没有
AutoReset
属性,因此我还将使用
lock
块来防止多个实例运行。很好的建议。由于
System.Windows.Forms.Timer
没有
AutoReset
属性,我还将使用
lock
块来防止多个实例运行。