Hi.
Thisis what i am trying to do.
I have a GUI that lets the user enter some values from which i generate a custom message script that is sent to an embedded board via the serial port. Think of the script as a old school basic program.
At time 0 do this, at time 10 do this, at time 30 do that, etc. Most of the time this is irrelevant to the user, but at certain times ( say 100, 150, 300, etc ) the embedded board commands voltage and things start happening for a few seconds, then move go.
I want to implement a countdown for the user to see on the screen, something like
"xxxxxx seconds until next event", etc. Since i know exactly when this will happen ahead of time ( i create the script ), how can i implement this.
I was thinking saving the each time an event will be fired in an array. The embedded board does sync back to me every .5 secs so i was thinking using that as my clock.
Will using a linklist or any other datastructure better ? or is there a more elegant way?
thanks!
| | DVAz Thursday, March 26, 2009 2:13 PM | Keeping time incrementally cannot work. There are small delays each time your tick event handler tocks and you'll gradually fall behind. As you already found out. You need an absolute clock reference. DateTime.Now works well, it has a resolution of a little over 15 msec. Either make the timestamps in your list of events absolute as well as compute relative time from some kind of absolute start time: private DateTime mStart; public void Run() { mStart = DateTime.Now; //... } private void timer1_Tick(..) { TimeSpan tick = DateTime.Now - mStart; // Search list for events >= tick //... } Hans Passant.- Marked As Answer byLinda LiuMSFT, ModeratorWednesday, April 01, 2009 7:23 AM
-
| | nobugz Saturday, March 28, 2009 2:07 PM | I'm not sure whether i got your problem right, but i will try with the understanding i have.
1. You can use Queue to add the time durations into the Queue when u generate the script.
2. Have a variable CountDown and initiallize to 0
3. You can have a timer contol with interval=1000 and in the tick event if the countDown is 0 then dequeue and put the value to Countdown Otherwise countdown-- and display it in a label.
Hopefully this is what you were expecting... | | ChronusDOTNet Friday, March 27, 2009 2:57 AM | Thanks for the help! Yeah you got the problem right.
1)Can't use a queue. Since the script is repeatable up to X times, i have to know when the script ends and set the times[] back to 0 and start over again.
2) Correct
3) I actually did this in an earlier release, the problem is that i used a windows form timer and since the scripts ran for days, the little loss of precision / time would make it pretty much be off by several minutes. That why i got the req this time to use the messages coming back to from the embedded device as my timer tick. ( think of it as an keep alive connection, every .5 secs i receive a message and i respond back ). the Idea is that i would set the countdown to 0 at the start of the script, then every "ticK", or message i received, i added .5 to it.
That seems like the general approach of this, was trying to gather different ideas. :)
| | DVAz Friday, March 27, 2009 4:47 AM | 3. So in the list instead of storing the duration in the queue/array you can store the pre-computed datetime and you can show the count down based on diff between the current clock time and the pre-computed time. And once the diff is <=0 you can move to the next datetime in the queue/array? | | ChronusDOTNet Saturday, March 28, 2009 3:27 AM | Keeping time incrementally cannot work. There are small delays each time your tick event handler tocks and you'll gradually fall behind. As you already found out. You need an absolute clock reference. DateTime.Now works well, it has a resolution of a little over 15 msec. Either make the timestamps in your list of events absolute as well as compute relative time from some kind of absolute start time: private DateTime mStart; public void Run() { mStart = DateTime.Now; //... } private void timer1_Tick(..) { TimeSpan tick = DateTime.Now - mStart; // Search list for events >= tick //... } Hans Passant.- Marked As Answer byLinda LiuMSFT, ModeratorWednesday, April 01, 2009 7:23 AM
-
| | nobugz Saturday, March 28, 2009 2:07 PM | Thaks for both your replies. So if i understand correctly ( sorry im still learning :) ), i am making my list of times of time Timespan objects? This is how it works right now, this is a sample of a serial message. Each < > is a byte. <0xAA<time1><time2><time3>.............. where time is a 24 byte value, in miliseconds. Right now i am just storing them in a int[] array. Again if read correctly the example above, what should the tick interval of the timer used? If my requirements are to use the embedded serial message back to me as a sync ( instead of using a timer_tick event, i would use the Data_Received event handler ) would it still work? Again thanks for the help!
| | DVAz Saturday, March 28, 2009 10:52 PM | I guess it would still work, so you will reset your Count Down counter based on the Data_Received event. | | ChronusDOTNet Monday, March 30, 2009 5:26 AM |
|