473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need design for calling a method at regular intervals

In our app, we need to collect data at regular intervals (4, 8 or 16
seconds - user settable). The collection happens in a background
thread. My first approach was to do the collection, which takes about
0.5 seconds, then calculate how much time to sleep until the next
collection.

The problem is that when I cancel the collection, I have to wait for it
to emerge from the sleep before it actually exits. I could call
thread.Abort, but that seems frowned up.

A second approach is to have the collection loop wait for an event.
The event could be signalled from a Threading.Timer object or from the
user request.

Can I have your opinion on either of these approaches, or any othe
suggestions?

Thanks

Mitch

Oct 11 '06 #1
4 2445
Not sure if I really understand what the issue is here. Why can't you just
call your collection method from within a timer's elapsed event handler? If
you need to cancel the operation, just set the timer to disabled, and
re-enable it on demand.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"ba*****@yahoo.com" wrote:
In our app, we need to collect data at regular intervals (4, 8 or 16
seconds - user settable). The collection happens in a background
thread. My first approach was to do the collection, which takes about
0.5 seconds, then calculate how much time to sleep until the next
collection.

The problem is that when I cancel the collection, I have to wait for it
to emerge from the sleep before it actually exits. I could call
thread.Abort, but that seems frowned up.

A second approach is to have the collection loop wait for an event.
The event could be signalled from a Threading.Timer object or from the
user request.

Can I have your opinion on either of these approaches, or any othe
suggestions?

Thanks

Mitch

Oct 11 '06 #2
ba*****@yahoo.com wrote:
In our app, we need to collect data at regular intervals (4, 8 or 16
seconds - user settable). The collection happens in a background
thread. My first approach was to do the collection, which takes about
0.5 seconds, then calculate how much time to sleep until the next
collection.

The problem is that when I cancel the collection, I have to wait for it
to emerge from the sleep before it actually exits. I could call
thread.Abort, but that seems frowned up.

A second approach is to have the collection loop wait for an event.
The event could be signalled from a Threading.Timer object or from the
user request.

Can I have your opinion on either of these approaches, or any othe
suggestions?

Thanks

Mitch
Hi Mitch,

You seem to have the basis for a good idea in your post; i.e. using a
signalled event. If you take a look at the AutoResetEvent class, part of
the System.Threading namespace, then you'll find it contains a Wait method.

The wait method blocks the calling thread, until it timesout, via the
timeout parameter, or until the AutoResetEvent is signalled, using the Set
method.

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 11 '06 #3
Mitch,

In addition to what Tom said the WaitOne method returns a bool
indicating why it returned. It will return true when the event is
signalled or false when the wait timesout. That will give you enough
information to determine whether you need to collect more data (because
of a timeout) or cancel the operation (because the event was
signalled).

If you need more complex event signalling then you can create multiple
WaitHandle object (either ARE or MRE) and use the WaitHandle.WaitAny
method which will block on an array of WaitHandle objects and return
the index of the event that was signalled.

Brian

Tom Spink wrote:
Hi Mitch,

You seem to have the basis for a good idea in your post; i.e. using a
signalled event. If you take a look at the AutoResetEvent class, part of
the System.Threading namespace, then you'll find it contains a Wait method.

The wait method blocks the calling thread, until it timesout, via the
timeout parameter, or until the AutoResetEvent is signalled, using the Set
method.

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 11 '06 #4
Brian Gideon wrote:
Mitch,

In addition to what Tom said the WaitOne method returns a bool
indicating why it returned. It will return true when the event is
signalled or false when the wait timesout. That will give you enough
information to determine whether you need to collect more data (because
of a timeout) or cancel the operation (because the event was
signalled).

If you need more complex event signalling then you can create multiple
WaitHandle object (either ARE or MRE) and use the WaitHandle.WaitAny
method which will block on an array of WaitHandle objects and return
the index of the event that was signalled.

Brian

Tom Spink wrote:
>Hi Mitch,

You seem to have the basis for a good idea in your post; i.e. using a
signalled event. If you take a look at the AutoResetEvent class, part of
the System.Threading namespace, then you'll find it contains a Wait
method.

The wait method blocks the calling thread, until it timesout, via the
timeout parameter, or until the AutoResetEvent is signalled, using the
Set method.

--
Hope this helps,
Tom Spink

Google first, ask later.
Ah man, I knew I forgot to mention something! Cheers for that Brian!

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 11 '06 #5

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

Similar topics

2
2306
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
3
3114
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
8
6611
by: Rakehs | last post by:
Hi, I have a method Show() which is invoked by several methods at different intervals. I want to identify the calling method within Show() whenever it is invoked. How do i do this? Thanks in...
1
1004
by: joe | last post by:
Hello A simply question which may not have a simple answer.. I would like to update an IE page at regular intervals to give clients visual feedback on say a download that there doing, or some...
3
2155
by: Peter Strøiman | last post by:
Hi. I have a web application that needs to run tasks at regular intervals. E.g. it must send out emails every night to people who subscribe to that service. I have come up with one solution,...
31
1614
by: cctv.star | last post by:
I was wondering, why you always have to remember to call bases' constructors explicitly from the derived class constructor? Why hasn't this been enforced by the language?
5
2019
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
10
2076
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
4
2279
by: arnaudk | last post by:
I am trying to come up with a class design to deal with asynchronous data to be stored and analyzed over multiple time frames and could really use some design input. This is a rather long question...
0
7033
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
6903
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
7027
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
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4468
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
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.