Thread safety with a timer

  • Thread starter Thread starter Antonio
  • Start date Start date
A

Antonio

Hello, I am using a timer object (System.Timers.Timer) in my windows
service. The timer will elapse every 5 minutes and look for a signal
that has arrived during the 5 minutes it was sleeping. If a signal has
arrived, the ElapsedEventHandler will kick off my method that starts
processing the task that it needs to do. But if the task takes 7
minutes to run, what happens at the 5 minute mark? I think it will
kick off on another thread. Is that right? The ElapsedEventArgs class
says

"Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe."

is there something I have to do to keep my windows service "thread
safe" or is it already thread safe?

All that the process does when it gets a signal is to run a long stored
procedure and place the results in a excel file, zip the file, and
email the file to the requester.

Thank you.
 
Antonio said:
Hello, I am using a timer object (System.Timers.Timer) in my windows
service. The timer will elapse every 5 minutes and look for a signal
that has arrived during the 5 minutes it was sleeping. If a signal has
arrived, the ElapsedEventHandler will kick off my method that starts
processing the task that it needs to do. But if the task takes 7
minutes to run, what happens at the 5 minute mark? I think it will
kick off on another thread. Is that right? The ElapsedEventArgs class
says

"Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe."

is there something I have to do to keep my windows service "thread
safe" or is it already thread safe?

All that the process does when it gets a signal is to run a long stored
procedure and place the results in a excel file, zip the file, and
email the file to the requester.

Stop the timer before you perform the task and start it again when you are
done.

David
 
Yes, the System.Timers.Timer will fire even if the previous timer event
is still being processed. So that means your ElaspedEventHandler might
get called in the middle of execution of the previous timer event.

It might lead to some problems though, while the previous timer elapsed
event handler is writing to the file, the next event might fire and
attempt to open the same file for writing..

Regards
Senthil
 
each process request will have a unique number (a request number) and I
will name the excel file and zip file with that number in the name, so
the names will be unique. would that clear up the problems you mention?
 
thank you but if my users submit some long reports I don't want to make
the last person have to wait it I can make the service run many reports
at once.
 
I guess it would, as long as you don't access/modify member variables
within your event handler.

Regards
Senthil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top