473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Timer or threading Q


Yello,

Quick Q:
I created a windows service that changes some data every (approx) 1sec,
but currently it is using a loop on a separate thread that uses
Thread.Sleep(1000).
Is that bad programming tecnique? Should I be using a timer instead? Is there
any good advantages to the way it is done?

A side Q:
I am currently working with the newest version of .net. In the previous
version,
I was running in to trouble using the System.Threading.Timer class. It would
only run for a certain number of call backs then would fail to run anymore.
I fixed that problem by going to a server timer. I haven't a clue why it kept
failing, and I dont know if it does such in the newer version. Can anyone
shead
some light on the matter??

Thanks in advance for any and all help.

Dec 30 '05 #1
8 1512
Without seeing how you are using the timer, I can't say why it would or
would not work. However, I do agree that using the timer is better than
creating a thread and then putting it to sleep every second.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TheMadHatter" <Th**********@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...

Yello,

Quick Q:
I created a windows service that changes some data every (approx) 1sec,
but currently it is using a loop on a separate thread that uses
Thread.Sleep(1000).
Is that bad programming tecnique? Should I be using a timer instead? Is
there
any good advantages to the way it is done?

A side Q:
I am currently working with the newest version of .net. In the previous
version,
I was running in to trouble using the System.Threading.Timer class. It
would
only run for a certain number of call backs then would fail to run
anymore.
I fixed that problem by going to a server timer. I haven't a clue why it
kept
failing, and I dont know if it does such in the newer version. Can anyone
shead
some light on the matter??

Thanks in advance for any and all help.

Dec 30 '05 #2
MJB

I had similar problems with the timer object so I changed my code to use
a while loop with a sleep delay, which was much more reliable. I'm not
sure if the timer has been modified in .Net 2.0 or not. I never did
figure out why the timer would stop firing.

Regarding your service...it's hard to say if it's bad technique. If you
don't care exactly when the timer fires, just that it fires every 5
seconds than what your doing is fine IMO. Good luck.

TheMadHatter wrote:
Yello,

Quick Q:
I created a windows service that changes some data every (approx) 1sec,
but currently it is using a loop on a separate thread that uses
Thread.Sleep(1000).
Is that bad programming tecnique? Should I be using a timer instead? Is there
any good advantages to the way it is done?

A side Q:
I am currently working with the newest version of .net. In the previous
version,
I was running in to trouble using the System.Threading.Timer class. It would
only run for a certain number of call backs then would fail to run anymore.
I fixed that problem by going to a server timer. I haven't a clue why it kept
failing, and I dont know if it does such in the newer version. Can anyone
shead
some light on the matter??

Thanks in advance for any and all help.

Dec 30 '05 #3
TheMadHatter <Th**********@discussions.microsoft.com> wrote:
Quick Q:
I created a windows service that changes some data every (approx) 1sec,
but currently it is using a loop on a separate thread that uses
Thread.Sleep(1000).
Is that bad programming tecnique? Should I be using a timer instead? Is there
any good advantages to the way it is done?
Well, it's simple. That's a pretty strong advantage in my view. So long
as you make sure that you keep going in the face of any exceptions (if
you want to), it seems an entirely reasonable way of working to me.
A side Q:
I am currently working with the newest version of .net. In the previous
version,
I was running in to trouble using the System.Threading.Timer class. It would
only run for a certain number of call backs then would fail to run anymore.
I fixed that problem by going to a server timer. I haven't a clue why it kept
failing, and I dont know if it does such in the newer version. Can anyone
shead some light on the matter??


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Without seeing how you are using the timer, I can't say why it would or
would not work. However, I do agree that using the timer is better than
creating a thread and then putting it to sleep every second.


Why, out of interest? Using a sleep seems inherently simpler to me. I
would find it easier to debug, too. There are times when timers are
useful, but in this case I think the sleep solution sounds like the
best one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #5

"TheMadHatter" <Th**********@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...

Yello,

Quick Q:
I created a windows service that changes some data every (approx) 1sec,
but currently it is using a loop on a separate thread that uses
Thread.Sleep(1000).
Is that bad programming tecnique? Should I be using a timer instead? Is
there
any good advantages to the way it is done?

A side Q:
I am currently working with the newest version of .net. In the previous
version,
I was running in to trouble using the System.Threading.Timer class. It
would
only run for a certain number of call backs then would fail to run
anymore.
I fixed that problem by going to a server timer. I haven't a clue why it
kept
failing, and I dont know if it does such in the newer version. Can anyone
shead
some light on the matter??

Thanks in advance for any and all help.


IMO it's the right choice:
It's simple, and it doesn't waste any more resources like timers.
Of course this doesn't solve the issue with the timer which should work as
well, in general this is due to the fact that you aren't holding a reference
to the timer or it's callback, that means that the timer or the callback
will be destroyed when the GC comes along, not sure though, only debugging
will tell

Willy.

Dec 30 '05 #6
> Why, out of interest? Using a sleep seems inherently simpler to me. I
would find it easier to debug, too. There are times when timers are
useful, but in this case I think the sleep solution sounds like the
best one.
The only reason I can think of for using a timer is when you need to perform
work at a specific timed interval. If the timing isn't critical, putting the
thread to sleep would work fine.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m... Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Without seeing how you are using the timer, I can't say why it would
or
would not work. However, I do agree that using the timer is better than
creating a thread and then putting it to sleep every second.


Why, out of interest? Using a sleep seems inherently simpler to me. I
would find it easier to debug, too. There are times when timers are
useful, but in this case I think the sleep solution sounds like the
best one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 30 '05 #7
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Why, out of interest? Using a sleep seems inherently simpler to me. I
would find it easier to debug, too. There are times when timers are
useful, but in this case I think the sleep solution sounds like the
best one.


The only reason I can think of for using a timer is when you need to perform
work at a specific timed interval. If the timing isn't critical, putting the
thread to sleep would work fine.


Yes - although even if you wanted to do it at intervals, you could
always sleep for a variable length of time instead of always waiting
for a second. (Work out when you should next fire, and subtract the
current time.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #8
Personally, I find the logic and semantics to be more clear when using a
timer. But that's just me. The idea of manually spawning a thread and
putting it to sleep, when there is something that does the same thing is a
bit redundant.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
> Why, out of interest? Using a sleep seems inherently simpler to me. I
> would find it easier to debug, too. There are times when timers are
> useful, but in this case I think the sleep solution sounds like the
> best one.


The only reason I can think of for using a timer is when you need to
perform
work at a specific timed interval. If the timing isn't critical, putting
the
thread to sleep would work fine.


Yes - although even if you wanted to do it at intervals, you could
always sleep for a variable length of time instead of always waiting
for a second. (Work out when you should next fire, and subtract the
current time.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 30 '05 #9

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

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
2
by: hnkien | last post by:
Hi, I am writing a windows service with threading.timer for 10 seconds but it didn't work. Here are my code: namespace SchedulerService { public class ScheduleService :...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
5
by: Dave | last post by:
I trying to setup a time. The following code is in a function, Timer stateTimer = new Timer(ab(), null, 1000, 1000); I have included using System.Threading; at the begining of my code....
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
11
by: Anil Gupte/iCinema.com | last post by:
When I use this Dim instance As New Timer I get the error: Error 1 Overload resolution failed because no accessible 'New' accepts this number of arguments. Yet, in the help section for...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.