473,397 Members | 1,961 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,397 software developers and data experts.

Seems to be a dumb question = Windows Services.

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
Nov 16 '05 #1
8 1202
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.

--
William Stacey, MVP

"BolineC" <cr***@craiganddenise.org> wrote in message
news:44**************************@posting.google.c om...
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


Nov 16 '05 #2
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

"William Stacey [MVP]" wrote:
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.

--
William Stacey, MVP

"BolineC" <cr***@craiganddenise.org> wrote in message
news:44**************************@posting.google.c om...
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


Nov 16 '05 #3
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?

"BolineC" wrote:
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

Nov 16 '05 #4
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" <Pa********@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?
"BolineC" wrote:
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


Nov 16 '05 #5
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" <cr***@craiganddenise.org> wrote in message
news:44**************************@posting.google.c om...
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

Nov 16 '05 #6
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
"William Stacey [MVP]" <st***********@mvps.org> wrote in message news:<O#**************@TK2MSFTNGP10.phx.gbl>...
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" <Pa********@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?

"BolineC" wrote:
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

Nov 16 '05 #7
I picked this up from the TCP stack implementation in the thee TCP/IP book.
That is how he does scheduled clean up jobs.

--
William Stacey, MVP

"BolineC" <cr***@craiganddenise.org> wrote in message
news:44**************************@posting.google.c om...
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
"William Stacey [MVP]" <st***********@mvps.org> wrote in message

news:<O#**************@TK2MSFTNGP10.phx.gbl>...
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" <Pa********@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?

"BolineC" wrote:

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


Nov 16 '05 #8
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 )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eX**************@TK2MSFTNGP11.phx.gbl...
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" <cr***@craiganddenise.org> wrote in message
news:44**************************@posting.google.c om...
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



Nov 16 '05 #9

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

Similar topics

0
by: Stefan Hinz | last post by:
Degan, jumping in to try and solve some problems that look pretty obvious to me ... > #options for default service (mysqld2) > (mysqld2) It should be , not (mysqld2).
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
2
by: Paul Fredlein | last post by:
Hi, Let's pretend, for a moment, that I've made the latest killer app using the ..net framework. The executable is sitting comfortably up there in the ether on a web server. Zillions of paying...
5
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
This code works fine in Windows Application. In Windows Application, I am able to zip the image files properly and it totally contains 900MB My problem is the same code which I used in my Windows...
2
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
1
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Overview NOTE- This complete article on "Windows Autorun FAQs" applies theoretically to all Windows NT-based OSes till Windows Vista (and probably Vista's successors too)....
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.