Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

vsto - Excel word, timer continues even if i turn off

I created a timer

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 method makes an api request every 30 seconds after the file is opened. There is nothing wrong but Even if I open 2 excel applications and close an excel application, it continues to make requests in the background. How can I turn off the timer after the application is closed?

This problem exists in word, excel and powerpoint.

question from:https://stackoverflow.com/questions/66046359/excel-word-timer-continues-even-if-i-turn-off

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to run your code in the timer_tick function rather than in the Document_Open function. I provided a code sample for you in your other question that effectively uses the timer_tick event.

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

And in your document_close event, you need to stop your timer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...