System.Timers.Timers + WindowsService

  • Thread starter Thread starter Björn
  • Start date Start date
B

Björn

Hi,
I have problems using the timer object in a C# Windows Service

In my OnStart() Method of the Windows Service I declare a timer object
"Timer myTimer = 0;"

Than I have a loop "for(i=0;i<=3;i++)" where I set "myTimer = new Timer;"
and the Eventhandler "myTimer.Elapsed += new
ElapsedEventHandler(OnTimedEvent);"

Now I have four different timers running. But only the last keep running,
the other three stops working after a while. And I don´t know why?
Can it be that I run into a thread problem?
All four timers call the same method (OnTimedEvent). First I thought I have
a synchronisation problem, so I set a Lock statement around the code of the
method

"private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
lock(lockvar)
{
//my code
}
}"

I don´t know what´s going wrong, has anyone an idea???


Best regards

Björn
 
Björn said:
Hi,
I have problems using the timer object in a C# Windows Service

In my OnStart() Method of the Windows Service I declare a timer object
"Timer myTimer = 0;"

Than I have a loop "for(i=0;i<=3;i++)" where I set "myTimer = new Timer;"
and the Eventhandler "myTimer.Elapsed += new
ElapsedEventHandler(OnTimedEvent);"

Now I have four different timers running. But only the last keep running,
the other three stops working after a while. And I don´t know why?
Can it be that I run into a thread problem?

No the problem is that you assign the 4 timers to the same variable...
So the GC is cleaning up the 3 that are created first. (no more references
to the first 3 created timers)
What you should do is put those 4 references into a array of Timers of an
ArrayList.

something like this:

Timer myTimer = 0;
ArrayList myList;

*** The for loop**
myTimer = new Timer
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);"
myArrayList.Add(myTimer);



Best Regards,

Jeroen Vandezande
 
Hi Jeroen

Thx for your answer. It sounds logic and it seems to work. All four timers
now working for above 15 minutes.

Can I remove the Lock statement from my OnTimedEvent method? All four timers
are running nearly at the same time. Every minute with a few milliseconds
difference.

thx

Björn
 
Björn said:
Hi Jeroen

Thx for your answer. It sounds logic and it seems to work. All four timers
now working for above 15 minutes.

Can I remove the Lock statement from my OnTimedEvent method? All four
timers
are running nearly at the same time. Every minute with a few milliseconds
difference.


I don't really know... As far as I know the Event of the timers is fired in
the main thread...
So I am not sure if they can run at the same time, I think he does them one
at a time...
Maybe someone else can help out here... I would like to know too.


Best Regards,

Jeroen Vandezande
 
Björn said:
Hi Jeroen

Thx for your answer. It sounds logic and it seems to work. All four timers
now working for above 15 minutes.

Can I remove the Lock statement from my OnTimedEvent method? All four
timers
are running nearly at the same time. Every minute with a few milliseconds
difference.

What does the code inside of the lock do? If the code isn't writing to any
common fields or properties and not calling methods that do so, you don't
need the lock. If you are doing writes to anything that isn't private to the
method you will probably have to use a lock, although perhaps not at the
granularity you are currently using.
 
Hi

The code inside the lock search a private static collection for data, than
calls another method which makes a select to a ms sql server. If the select
returns data a Webservice is called by a Proxy class which is loaded
dynamical.

I believe I will continue using the lock statement, so I can´t run in
trouble.

Björn
 
Back
Top