Need some tips on how to implement a timer

G

Guest

Hey guys,

Here is what I am trying to achieve:

I have a grid, and every once in a while the grid will receive a message to
add a new row and highlight it (change the backcolor) for five minutes.
After the five minutes has passed, unhighlight it. The grid needs to
highlight any rows that have arrived in the last five minutes.

Here is how I handle this now. Each row has a value indicating its arrival
time and I have a timer that ticks every 10 seconds. On the timer tick
event, I go through every row in the grid and check if it is highlighted. If
it is, I compare its arrival time to the current time and if the difference
is >= five minutes, I unhighlight it.

I would like the highlighting and unhighlighting to occur as accurately as
possible but ticking every second instead of 10 (or any other higher value)
would loop through all of the rows in the grid every second and that would be
slow since the number of rows may be high.

Does anyone have any other possible suggestions as to how to handle this
scenario?
It would be great if each row can monitor itself and then unhighlight itself
after five minutes but I'm not sure if that is possible.

Thanks for the help,
-Flack
 
V

vj

You can try this method... have like member variables ( in the class that
carries your grid) that indicate last update time, rowIDs
updated/highlighted. So in row update event you can highlight the current
row that came in and just loop through the previously highlighted rows and
remove them if your condition matches...

HTH
VJ
 
K

Kevin Spencer

Hi Flack,

A lot depends on what you mean by "grid." There is no class by that name.
Does anyone have any other possible suggestions as to how to handle this
scenario?
It would be great if each row can monitor itself and then unhighlight
itself
after five minutes but I'm not sure if that is possible.

I think you're basically on the right track there. You have a few
alternatives. One would be to inherit whatever class the "grid" row is, and
add a timer to that class which is started when the row is created, and
fires its elapsed event after a configurable interval (5 minutes by default,
but you always want to be able to change it).

When the timer fires its event, it is stopped and disposed, so that it only
fires once, when it is needed. Again, depending upon the "grid" you're
working with, the inherited row class could either unhighlight itself, or
raise an event that signals the "grid" to unhighlight it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
G

Guest

Thanks for the reply vj.

Your method would work fine if I only needed to change the highlighting when
a row is added. However I can't use the row update event because I may have
received, let's say, 10 rows at once, and then no more rows for a long time.
I still need to be able to unhighlight those 10 rows after five minutes have
passed since they arrived.

I'm starting to think that using a timer to check the rows at certain
intervals is the only way.
 
G

Guest

Sorry about that.

The "grid" I am using is a FlexGrid for .NET control, from ComponentOne.
It's like .NETs standard DataGrid but with more features.

I have a data table that is bound to the grid and the data table is where
the rows come in. I'm assuming that any method used to solve this problem
would work the same regardless of whether I am using a C1 FlexGrid or a
regular .NET DataGrid.
 
W

William Stacey [MVP]

The System.Threading.Timer class is well suited for this task. For each row
added, create a new timer can set it to fire once in 5 minutes like:
Timer t = new Timer(callback, yourRowObject, TimeSpan.FromMinutes(5),
Timeout.Infinite);

Add the timer object to a row Tag or something. When it fires, your row
object will be passed to your callback and you can unhightlight the row and
dispose() the timer object. That way you don't poll or spin and your rows
will unhightlight as close to exactly five minutes as you can get.
Internally, this uses a timer queue so only the "nearest" timer is waiting,
so this can scale to 100's of thousands of timers and works well.

--
William Stacey [MVP]

| Hey guys,
|
| Here is what I am trying to achieve:
|
| I have a grid, and every once in a while the grid will receive a message
to
| add a new row and highlight it (change the backcolor) for five minutes.
| After the five minutes has passed, unhighlight it. The grid needs to
| highlight any rows that have arrived in the last five minutes.
|
| Here is how I handle this now. Each row has a value indicating its
arrival
| time and I have a timer that ticks every 10 seconds. On the timer tick
| event, I go through every row in the grid and check if it is highlighted.
If
| it is, I compare its arrival time to the current time and if the
difference
| is >= five minutes, I unhighlight it.
|
| I would like the highlighting and unhighlighting to occur as accurately as
| possible but ticking every second instead of 10 (or any other higher
value)
| would loop through all of the rows in the grid every second and that would
be
| slow since the number of rows may be high.
|
| Does anyone have any other possible suggestions as to how to handle this
| scenario?
| It would be great if each row can monitor itself and then unhighlight
itself
| after five minutes but I'm not sure if that is possible.
|
| Thanks for the help,
| -Flack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top