c# - Date comparison within timer -


i have timer runs every second, code bellow:

    public mainwindow()     {         initializecomponent();          dispatchertimer timer = new dispatchertimer();         timer.interval = timespan.frommilliseconds(1000);         timer.tick += new eventhandler(someeventhandler);         timer.start();     }      private void someeventhandler(object sender, eventargs args)     {         tvmonitor tvmonitor = new tvmonitor();         if (tvmonitor.needsturningon())         {             console.writeline("on");             tvcom.sendcommand(settingmanager.gettvcode("on") + environment.newline);         }     } 

in function needsturningon() have following:

    public bool needsturningon()     {         var turnon = (from settings in context.systemsettings                       settings.systemsettingname == "tvontime"                       select settings).first();         if (turnon.systemsettingvalue == string.empty)         {             return false;         }          datetime date = convert.todatetime(turnon.systemsettingvalue);         console.writeline("turnontime: " + date);         console.writeline("currenttim: " + datetime.now);         if (date != datetime.now)         {             console.writeline("false");             return false;         }         else         {             console.writeline("true");             return true;         }     } 

now in console using debugging, have following output:

... turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:07 false turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:08 false turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:09 false turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:10 false turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:11 false turnontime: 11/04/2011 14:05:10 currenttim: 11/04/2011 14:05:12 false ... 

why still firing off false instead of true once date same?

you compare times every second - milliseconds still different (especially assuming turn-on time compare doesn't use milliseconds). compare in second resolution this:

datetime currenttime = datetime.now; currenttime = currenttime.addmilliseconds(-currenttime.millisecond); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -