【问题标题】:Run function every 10 seconds [duplicate]每10秒运行一次功能[重复]
【发布时间】:2014-04-22 15:14:35
【问题描述】:

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

【问题讨论】:

标签: c# winforms


【解决方案1】:

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

【讨论】:

  • 好建议。由于System.Windows.Forms.Timer 没有AutoReset 属性,我还将使用lock 块来防止多个实例运行。
【解决方案2】:

关注这个:

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

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多