Concurrent Timers...

B

Blue Streak

Hi, Folks!

I have a question about concurrent timers. In my current project
I am writing an NT Service (I know that is old terminology but I work
with idiots who think you mean Web Service when you say Service) that
scans an database table. I have one timer setup to scan about every
15 seconds for new records, does its number crunching for that record,
then marks it as processed with Now() in a DateTime field.

The first timer / process is working fine. However, I wanted to
add some maintenance into the NT Service such that the database table
doesn't grow too large. My idea was to run a second timer set to scan
the table every 12 or 24 hours and check the age of the record based
on the Processed field. When the record is of a certain age (e.g. 5
days) then it is deleted or archived depending on what my boss wants.

I have been trying to think of a scheme that prevents both jobs
from running concurrently. If one process is running how do you get
the other process to wait until the other is finished?

I don't think adding the line Timer.Enable = false would cut it
because it would reset the timer back to zero. Using that on the
first timer is fine because it runs every 15 seconds. However, using
that technique on the second timer would be a problem because it would
end up never running.

Any ideas?

TIA...
 
B

Blue Streak

Hi, Folks!

    I have a question about concurrent timers.  In my current project
I am writing an NT Service (I know that is old terminology but I work
with idiots who think you mean Web Service when you say Service) that
scans an database table.  I have one timer setup to scan about every
15 seconds for new records, does its number crunching for that record,
then marks it as processed with Now() in a DateTime field.

    The first timer / process is working fine.  However, I wanted to
add some maintenance into the NT Service such that the database table
doesn't grow too large.  My idea was to run a second timer set to scan
the table every 12 or 24 hours and check the age of the record based
on the Processed field.  When the record is of a certain age (e.g. 5
days) then it is deleted or archived depending on what my boss wants.

    I have been trying to think of a scheme that prevents both jobs
from running concurrently.  If one process is running how do you get
the other process to wait until the other is finished?

    I don't think adding the line Timer.Enable = false would cut it
because it would reset the timer back to zero.  Using that on the
first timer is fine because it runs every 15 seconds.  However, using
that technique on the second timer would be a problem because it would
end up never running.

Any ideas?

TIA...

Forgot the details:
- .NET Framework 2.0
- System.Timers.Timer
- WinXP-SP3
 
B

Blue Streak

You put in a lock:

Add a member to your service class:

Private Shared ReadOnly SyncObject = new Object()

Then, in your Timer methods:

Sub TimerMethod1(....)
        SyncLock (SyncObject)
                ' do your stuff
        End SyncLock
End Sub

Sub TimerMethod2(....)
        SyncLock (SyncObject)
                ' do your stuff
        End SyncLock
End Sub

Now, if TimerMethod1 is in the lock, then timermethod2 will block at the
SyncLock statement until TimerMethod1 exists the lock.  That works the other
way as well.  You could make this a little more sophisticated if it's needed
(like timeouts on the locks, deadlock detection, etc) - but, I think this
should suffice for the simple example you've given :)

Hey, thanks!
 

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

Similar Threads


Top