Seems to be a dumb question = Windows Services.

B

BolineC

I am trying to use a timer in a simplistic windows service... In my
OnStart method, I declare and initialize the System.Threading.Timer,
and set it to fire every 30 seconds. In my callback method, I read
from one Database, and write to another. Mainly that is all it does.
(This only takes around 5 seconds to complete)

So, my problem is... All this is only happening once! My timer only
callsback one time. I am wondering if this is because my reference to
it drops out of scope into GC land? If that is the case, with such a
simple app, how can I keep it in scope?

Craig
 
W

William Stacey [MVP]

Instead of a timer your may just want a worker thread in a simple loop that
sleeps at the bottom of the loop for 30 seconds. Less work, clean, and you
don't need to fight async delegates, scope, etc., etc. Just a thought.
 
G

Guest

I am just learning c# (Converted from VB.Net), and am working on Service Apps. Can you post an example on what you mean.

Glenn
 
G

Guest

I use a System.Timers.Timer in my windows service, but in the fired event I stop and dispose the timer, do what is needed and then set up a new one. The reason for this is I'm not sure how long my process will actually take. Just an option?
 
W

William Stacey [MVP]

This would be another benefit of using a single thread with sleep instead.
If the proc took longer then 30 seconds to return, you don't sleep, but just
keep running job(s) Moreover, you don't have to worry about overlapped
timer events (as you said) running your method which could get messy
depending on what the method does. Then again, that may be exactly what you
want - depend on the app. I get uneasy feeling thinking about queued up
timer events on timer method... Maybe it is not an issue. Cheers.

--
William Stacey, MVP

Paul Ledger said:
I use a System.Timers.Timer in my windows service, but in the fired event
I stop and dispose the timer, do what is needed and then set up a new one.
The reason for this is I'm not sure how long my process will actually take.
Just an option?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I use William's advice, this is a piece of the code I'm using, just enough
for show how:

volatile bool mustDie = false;
object syncDie = new object();
protected override void OnStart(string[] args)
{
new Thread( new ThreadStart( Worker() )).Start();
}
protected override void OnStop()
{
lock( syncDie)
{
mustDie = true;
}
}
void Worker()
{
while( true )
{
if ( mustDie )
return; //Just finish the thread
// Do your stuff
//sleep for a while :)
Thread.Sleep( 3000 );
}
}


If you really need to use a timer, you should disable it while you are
running the code, then re-enable it.
Also make sure that if you are using the System.Timers.Timer you set the
AutoReset to true


Cheers,
 
B

BolineC

Speeking of "I get uneasy feeling thinking about queued up timer
events on timer method... "

How many events can be queued?

ALSO, Thanks a lot for the advice, I will be doing exactly what you
have recomended. I just get stuck in a rut - need something to occur
every 30 secs, that meeans timer! :)

Craig
 
W

William Stacey [MVP]

I picked this up from the TCP stack implementation in the thee TCP/IP book.
That is how he does scheduled clean up jobs.
 
W

William Stacey [MVP]

From quick review, that is what I was thinking. He could also add the
TimeSpan to figure out if he needed to sleep or go round again. Cheers! :)

--
William Stacey, MVP

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I use William's advice, this is a piece of the code I'm using, just enough
for show how:

volatile bool mustDie = false;
object syncDie = new object();
protected override void OnStart(string[] args)
{
new Thread( new ThreadStart( Worker() )).Start();
}
protected override void OnStop()
{
lock( syncDie)
{
mustDie = true;
}
}
void Worker()
{
while( true )
{
if ( mustDie )
return; //Just finish the thread
// Do your stuff
//sleep for a while :)
Thread.Sleep( 3000 );
}
}


If you really need to use a timer, you should disable it while you are
running the code, then re-enable it.
Also make sure that if you are using the System.Timers.Timer you set the
AutoReset to true


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


BolineC said:
I am trying to use a timer in a simplistic windows service... In my
OnStart method, I declare and initialize the System.Threading.Timer,
and set it to fire every 30 seconds. In my callback method, I read
from one Database, and write to another. Mainly that is all it does.
(This only takes around 5 seconds to complete)

So, my problem is... All this is only happening once! My timer only
callsback one time. I am wondering if this is because my reference to
it drops out of scope into GC land? If that is the case, with such a
simple app, how can I keep it in scope?

Craig
 

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