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

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 1505
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.