Trigger a function based on time of day
#1
Posted 11 September 2012 - 01:43 PM
#2
Posted 11 September 2012 - 03:43 PM
#3
Posted 12 September 2012 - 06:06 PM
TimeSpan ts; DateTime ShiftStart = DateTime.Today + new TimeSpan(16, 0, 0); //schedule first event at 16:00:00 today while (true) { DateTime now = DateTime.Now; ts = shft.ShiftStart.Subtract(now); if (ts.Ticks < 0 ) { //current time greather than event time, fire Thread t = new Thread(MyWorker); t.Start(); //set event time to tomorrow ShiftStart = ShiftStart + new TimeSpan(1, 0, 0, 0); } Thread.Sleep(10000); }
#4
Posted 13 September 2012 - 01:54 AM
#5
Posted 15 September 2012 - 02:24 PM
#6
Posted 15 September 2012 - 09:03 PM
I don't believe DateTime.Now() actually returns the current time as it does on the non-micro version of the .Net framework. The Netduino simply doesn't remember the system time when powered off (no battery) so you get a default value.
You need to somehow set the system time when powered up - maybe from a time server - then that code should work. I've thought about this a bit but have never worked through a solution but it seems likely that someone would have. Sorry I can't be more help.
You can do it via Time server or you can add a RTC Module (Real Time Clock). Set it once and forget it.
#7
Posted 16 September 2012 - 01:49 PM
using System; using Microsoft.SPOT; using System.Threading; namespace DoThingAtEleven { public class Program { public static void Main() { Debug.Print(DateTime.Now.ToString("HH:mm:ss")); DateTime needEventAt = DateTime.Today + new TimeSpan(23, 00, 0); long ticksToEleven = needEventAt.Ticks - DateTime.Now.Ticks; int millisecondsToEleven = (int)(ticksToEleven / TimeSpan.TicksPerMillisecond); Timer triggerAtEleven = new Timer(new TimerCallback(DoThingAtEleven), null, millisecondsToEleven, Timeout.Infinite); Thread.Sleep(Timeout.Infinite); } private static void DoThingAtEleven(object state) { Debug.Print("It's time!"); Debug.Print(DateTime.Now.ToString("HH:mm:ss"); } } }
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users