Reputation: 300
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.
Upvotes: 0
Views: 24
Reputation: 204
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.
Upvotes: 1