473,669 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raising an event at t0+dt

Hi there,

I have to write an algorithm in .NET/C# which perform some task but has to
be stopped after an intreval of dT seconds. Something like:

dT = 30sec;
for (;;) {
t0 = GetCurrentTime ();
for (i=0;...) {
perform some operations;
if ( GetCurrentTime () t0 + dT)
break;
}
}

I intend to raise an event at t0+dT, but I don't know if it is posssible and
the best solution...

Any idea ?

Oriane

Jun 5 '07 #1
22 1276
On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <or****@guerman tes.frwrote:
I have to write an algorithm in .NET/C# which perform some task but has
to be stopped after an intreval of dT seconds. Something like:

dT = 30sec;
for (;;) {
t0 = GetCurrentTime ();
for (i=0;...) {
perform some operations;
if ( GetCurrentTime () t0 + dT)
break;
}
}

I intend to raise an event at t0+dT, but I don't know if it is posssible
and the best solution...
Make "dT" a TimeSpan value, set it to 30 seconds, then make "t0" a
DateTime value, setting it to DateTime.Now before the loop. Then of
course use DateTime.Now in the other place you have "GetCurrentTime ", and
you're all set.

I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing, but assuming your "perform some
operations" is non-trivial, I think it's fine to just poll the current
time between each operation.

Pete
Jun 5 '07 #2
Hello Peter,

"Peter Duniho" <Np*********@nn owslpianmk.coma écrit dans le message de
news:op******** *******@petes-computer.local. ..
On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <or****@guerman tes.frwrote:
I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing,
Yes anyway I will have a multi-threading application. I thought of using a
Timer, but I would have to destroy it after the first signal...
but assuming your "perform some operations" is non-trivial, I think it's
fine to just poll the current time between each operation.
So you are probably right, but I must add that the "perform some operations"
won't take me too long. What I mean is that if I wait for t0+dt, I will have
to make the thread sleep most of the time in the for loop. That's why it
would be more elegant to "prepare" an event to be raised at t0+dt before the
loop, quit the loop after the operations have been performed (so in most of
the case), then catch the event to close the job... So the Timer is a good
solution...

Pete

Jun 5 '07 #3
On Jun 5, 12:22 pm, "Oriane" <ori...@guerman tes.frwrote:
Hello Peter,
On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <ori...@guerman tes.frwrote:
I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing,

Yes anyway I will have a multi-threading application. I thought of using a
Timer, but I would have to destroy it after the first signal...
It's still going to be easier not to make this particular part single
threaded, I'd have thought.

....but assuming your "perform some operations" is non-trivial, I
think it's
fine to just poll the current time between each operation.

So you are probably right, but I must add that the "perform some
operations" won't take me too long. What I mean is that if I wait for t0+dt, I
will have to make the thread sleep most of the time in the for loop.
It's not clear what you want to happen at t0+dt. If the point was just
to make it a timeout - i.e. don't keep going if you get to that time -
then you don't need to sleep. Just put the check in after each
iteration of the loop, bailing out if you've reached that maximum
time. If you happen to finish before that time, I'd expect you to be
able to just return anyway.

If this isn't the case, perhaps you could give us some more
information?
That's why it would be more elegant to "prepare" an event to be raised at
t0+dt before the loop, quit the loop after the operations have been
performed (so in most of the case), then catch the event to close the job...
So the Timer is a good solution...
That sounds significantly more complicated to me - a solution in
search of a harder problem than you're really trying to solve.

Jon

Jun 5 '07 #4

"Oriane" <or****@guerman tes.frwrote in message
news:A9******** *************** ***********@mic rosoft.com...
Hi there,

I have to write an algorithm in .NET/C# which perform some task but has to
be stopped after an intreval of dT seconds. Something like:

dT = 30sec;
for (;;) {
t0 = GetCurrentTime ();
for (i=0;...) {
perform some operations;
if ( GetCurrentTime () t0 + dT)
break;
}
}

I intend to raise an event at t0+dT, but I don't know if it is posssible
and the best solution...
I'd use a waitable timer, but that's not really a .NET-centric solution.
>
Any idea ?

Oriane

Jun 5 '07 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co ma écrit dans le message de
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
On Jun 5, 12:22 pm, "Oriane" <ori...@guerman tes.frwrote:
It's still going to be easier not to make this particular part single
threaded, I'd have thought.
I have to use multi-thread since my app will be sent message from other
apps, with WCF. The "some operations" will mainly wait for these messages...
It's not clear what you want to happen at t0+dt. If the point was just
to make it a timeout - i.e. don't keep going if you get to that time -
then you don't need to sleep. Just put the check in after each
iteration of the loop, bailing out if you've reached that maximum
time. If you happen to finish before that time, I'd expect you to be
able to just return anyway.
Yes you are absolutely right, I'm not clear. I give you the context: my .NET
app is running on a PC which connected with several electronic devices.
These devices send data about "tags" on the fly. Each tag is RFID and will
send periodically (say every dT=30 seconds, but this depends on the tag !) a
signal, which will be processed on the devices and then immedialtely
transfered to the PC.

On the PC, I have to process each tag separately, mainly to conclude
something like: this tag was at position X,Y at time t0. Of course this
processing must be stopped after dT seconds (the period of the RFID) for
each tag (depending on the tag),

Now, what make things more complex, I have several PC, and a tag signal can
be received by several PC. So PC have to communicate with each other to
"make a agreement" on the position of the tage at t0, since there is always
a "noise" into the signal (and so a discrepancy between the PC), and the
calculated position is different according to each PC.

So, starting from T0 , I wait for the signal of the devices (plugged on the
USB), and from the other PC (with WCF over TCP or HTTP), I first recognize
the tag (so now I know dT, and I can eventually set a Timer), I make a very
short operations on these messages, and at t0+dT, I stop the processing.

Now I've tried to design an UML activity and sequence diagrams, but the fact
that there are several PC and a different period for each RFID prevents me
to finish it up to now (I keep trying !!).
>
If this isn't the case, perhaps you could give us some more
information?
I hope this was enough...

Oriane

Jun 5 '07 #6
On Jun 5, 3:33 pm, "Oriane" <ori...@guerman tes.frwrote:
It's still going to be easier not to make this particular part single
threaded, I'd have thought.

I have to use multi-thread since my app will be sent message from other
apps, with WCF. The "some operations" will mainly wait for these
messages...
That doesn't mean that the bit which knows how long to process for
needs to be multi-threaded though.
It's not clear what you want to happen at t0+dt. If the point was just
to make it a timeout - i.e. don't keep going if you get to that time -
then you don't need to sleep. Just put the check in after each
iteration of the loop, bailing out if you've reached that maximum
time. If you happen to finish before that time, I'd expect you to be
able to just return anyway.

Yes you are absolutely right, I'm not clear. I give you the context: my .NET
app is running on a PC which connected with several electronic devices.
These devices send data about "tags" on the fly. Each tag is RFID and will
send periodically (say every dT=30 seconds, but this depends on the tag !)
a signal, which will be processed on the devices and then immedialtely
transfered to the PC.

On the PC, I have to process each tag separately, mainly to conclude
something like: this tag was at position X,Y at time t0. Of course this
processing must be stopped after dT seconds (the period of the RFID) for
each tag (depending on the tag),
Okay, that all sounds okay - but I'm not sure what's wrong with the
simple approach of:

1) Work out how long you're allowed to process for
2) For each iteration of the loop check whether you're still allowed
to process
3) Terminate when either you've finished processing, or the time
allotted has expired

It sounds an awful lot easier than using a timer, personally.

If that doesn't satisfy the requirements, could you give more details
about where it fails?

<snip>

Jon

Jun 5 '07 #7

"Jon Skeet [C# MVP]" <sk***@pobox.co ma écrit dans le message de
news:11******** **************@ w5g2000hsg.goog legroups.com...
On Jun 5, 3:33 pm, "Oriane" <ori...@guerman tes.frwrote:
It's still going to be easier not to make this particular part single
threaded, I'd have thought.

I have to use multi-thread since my app will be sent message from other
apps, with WCF. The "some operations" will mainly wait for these
messages...

That doesn't mean that the bit which knows how long to process for
needs to be multi-threaded though.
Right
>
>Yes you are absolutely right, I'm not clear. I give you the context: my
.NET
app is running on a PC which connected with several electronic devices.
[snip]
>
Okay, that all sounds okay - but I'm not sure what's wrong with the
simple approach of:

1) Work out how long you're allowed to process for
2) For each iteration of the loop check whether you're still allowed
to process
I was afraid that comparing the current date and t0+dT would be "CPU
consuming" ?
3) Terminate when either you've finished processing, or the time
allotted has expired

It sounds an awful lot easier than using a timer, personally.
Ok.
If that doesn't satisfy the requirements, could you give more details
about where it fails?
No your solution meets my requirements. By the way, when I perform a
Thread.Sleep () operation on a thread, do I consume CPU resources ?
<snip>

Jon
Jun 5 '07 #8
On Jun 5, 4:11 pm, "Oriane" <ori...@guerman tes.frwrote:
If that doesn't satisfy the requirements, could you give more details
about where it fails?

No your solution meets my requirements. By the way, when I perform a
Thread.Sleep () operation on a thread, do I consume CPU resources ?
Negligible, if any. But nothing in my recommended solution required a
sleep, as far as I could see...

Jon

Jun 5 '07 #9

"Jon Skeet [C# MVP]" <sk***@pobox.co ma écrit dans le message de
news:11******** **************@ g4g2000hsf.goog legroups.com...
On Jun 5, 4:11 pm, "Oriane" <ori...@guerman tes.frwrote:
Negligible, if any. But nothing in my recommended solution required a
sleep, as far as I could see...
Yes but I will have to use it for the thread which will wait for a WCF
message (i.e wait for a attribute to change), or for the other thread which
will wait for the USB data.
Jon
Jun 5 '07 #10

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

Similar topics

22
1402
by: Oriane | last post by:
Hi there, I have to write an algorithm in .NET/C# which perform some task but has to be stopped after an intreval of dT seconds. Something like: dT = 30sec; for (;;) { t0 = GetCurrentTime (); for (i=0;...) { perform some operations;
0
8462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7405
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
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 we have to send another system
2
2028
muto222
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.