【问题标题】:Excel word, timer continues even if i turn offExcel word,即使我关闭计时器也会继续
【发布时间】:2021-02-04 13:11:30
【问题描述】:

我创建了一个计时器

void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
{
                System.Timers.Timer timer=new System.Timers.Timer(30000);
                timer.Elapsed += (sender, e) => ExcelFileSendToAPI(Doc.FullName);
                timer.Start();
}

ExcelFileSendToAPI 方法在文件打开后每 30 秒发出一次 api 请求。 没有错 但是即使我打开 2 个 excel 应用程序并关闭一个 excel 应用程序,它仍然会继续在后台发出请求。 应用关闭后如何关闭定时器?

word、excel、powerpoint都存在这个问题。

【问题讨论】:

    标签: timer vsto office-addins excel-addins word-addins


    【解决方案1】:

    您需要在 timer_tick 函数而不是 Document_Open 函数中运行代码。我在您的其他问题中为您提供了一个代码示例,该示例有效地使用了 timer_tick 事件。

    Timer Timer1 = new Timer();
    
    void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
     {
                InitializeTimer();
    }
    
    void InitializeTimer()
    {
         Timer1.Interval = 30000;
         Timer1.Tick += new EventHandler(Timer1_Tick);
    }
    
    private void Timer1_Tick (Object sender, EventArgs e)
    {
         DocumentEditingSendToServer(Doc.Application.ActiveDocument);
    }
    

    在您的 document_close 事件中,您需要停止计时器。

    【讨论】:

      猜你喜欢
      • 2016-05-20
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2020-12-05
      相关资源
      最近更新 更多