473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Concurrent Timers...

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...
Aug 15 '08 #1
4 1259
On Aug 15, 12:30*pm, Blue Streak <rdlebre...@hotmail.comwrote:
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
Aug 15 '08 #2
On 2008-08-15, Blue Streak <rd********@hotmail.comwrote:
On Aug 15, 12:30*pm, Blue Streak <rdlebre...@hotmail.comwrote:
>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
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 :)

--
Tom Shelton
Aug 15 '08 #3
Blue Streak wrote:
On Aug 15, 12:30 pm, Blue Streak <rdlebre...@hotmail.comwrote:
>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
My preference is to use a single timer, in your case set at 15 secs. Each time
it fires, disable it, do your business, then start it again. For the 12 or 24
hour activity, just look at the current time, and compare it to some stored time
(a constant or config setting). When the time comes, do that activity instead.
This treats the occasional event more like an alarm clock, so it runs at say
11:00 pm each night, regardless of when the service last got restarted due to
whatever.
Aug 16 '08 #4
On Aug 15, 3:29*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
On 2008-08-15, Blue Streak <rdlebre...@hotmail.comwrote:


On Aug 15, 12:30*pm, Blue Streak <rdlebre...@hotmail.comwrote:
Hi, Folks!
* * I have a question aboutconcurrenttimers. *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 cutit
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

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 :)

--
Tom Shelton- Hide quoted text -

- Show quoted text -
Hey, thanks!
Aug 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: GDumencu | last post by:
I have a C# program that have to run all the time. For some reasons I cannot make it a service. This program has 3 timers and by time to time ( days or weeks) one of them stops. Some times, starts...
0
by: Dmitry Demchuk | last post by:
Hi everybody. Recently I ran into situation with System.Threading.Timer in my ASP.NET application. I reinstalled Windows on one of my servers and got timers stop firing events after while, they...
0
by: Cider123 | last post by:
I was originally using: System.Windows.Forms.Timer It started to lock my Window Service up, so I went to the next evolution: System.Threading.Timer All was good. I was using it to send...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
1
by: Jonathan Woods | last post by:
Hi there, I have three methods these need to execute at every interval time. I would like to know which option is better? Option A) Three System.Timers.Timer objects these execute each...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.