Services and Service timers

J

Jason

Hi all

just a quick question. I have a windows service with a "Catalogue" class.

I would like to know: if i put two "ServiceTimer" objects in my service
1. Do they run in separate threads (i have never threaded before, so
excuse my ignorance)
2. If i have instances of the catalogue class in both those timers, will
there be an error?
for example, if Timer1 loops every 10 mins, and Timer2 loops every 3
minutes, if at any one time they overlap, would it throw an exception?
3. Should I have a global variable for my catalogue class instance, or
should they be local to each timer?

Thanks
Jason
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Jason,

To the best of my knowledge, Service Timers just queue the specified event
handler to be called by a thread from the thread pool. Therefore, you can
have multiple service timers as long as your thread pool has enough free
threads to serve all timer handlers. This is my answer to #1 and #2.

As for #3, you can have a single instance of the Catalogue class, but this
instance should be thread-safe. This at least means all access to the
instance members should be protected with the "lock" statement.
 

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

Top