C# How to run code at given time? -
to put simply,
i start running c# program in morning, , program should show user message @ 5:45 pm. how can in c#?
edit: asked question because thought using timer not best solution (comparing current time periodically time need run task):
private void timerdowork_tick(object sender, eventargs e) { if (datetime.now >= _timetodowork) { messagebox.show("time go home!"); timerdowork.enabled = false; } }
i asked question because thought using timer not best solution (comparing current time periodically time need run task)
why? why not timer best solution? imo timer best solution. not way have implemented. try following.
private system.threading.timer timer; private void setuptimer(timespan alerttime) { datetime current = datetime.now; timespan timetogo = alerttime - current.timeofday; if (timetogo < timespan.zero) { return;//time passed } this.timer = new system.threading.timer(x => { this.showmessagetouser(); }, null, timetogo, timeout.infinitetimespan); } private void showmessagetouser() { if (this.invokerequired) { this.invoke(new methodinvoker(this.showmessagetouser)); } else { messagebox.show("your message"); } }
use this
setuptimer(new timespan(17, 45, 00));
Comments
Post a Comment