Tuesday 15 January 2013

.net - I have a C# Windows Forms Application and I want the user to be able to schedule a recurring task -



.net - I have a C# Windows Forms Application and I want the user to be able to schedule a recurring task -

my application sends out email based on info entered form on ui. ui allows them "schedule" job. don't want utilize outside classes, e.g. quartz. don't want utilize task scheduler require rewriting app... understand can done timers, , have attempted utilize windows.forms.timer, however, not getting desired results. want run @ user specified interval , time, until set end date.

private void submit_btn_click(object sender, eventargs e) { bool run = false; location_alert_timer.tick += new eventhandler(location_alert_timer_tick); if (schedule_chk.checked == true) { run = true; if (recur_txt.text != "" || recur_txt.text != "0") { while (run == true) { if ((convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text))) { //execute_command(); datetime dt1; datetime dt2; timespan ts = new timespan(convert.toint16(recur_txt.text),0,0,0); //timespan ts = new timespan(0,0,30); dt1 = convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")); dt2 = convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text); if (dt1 >= dt2) { execute_command(); location_alert_timer.interval = (convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text + ":00.000").add(ts) - datetime.now).milliseconds; location_alert_timer.start(); while (convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text)) { application.doevents(); } } else { location_alert_timer.interval = (convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text + ":00.000") - datetime.now).milliseconds; location_alert_timer.start(); while (exitflag == false) { application.doevents(); } } } else { location_alert_timer.stop(); run = false; } } } else { execute_command(); } } else { execute_command(); } } void location_alert_timer_tick(object sender, eventargs e) { if (convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text)) { execute_command(); } else { exitflag = true; } }

my timer events not seem firing right , know because have set them incorrectly...

here form ui:

if has similar implementation in future, @itsmatt's explanation led me rewrite whole event, ground up...below final implementation, , works fantastically:

private void submit_btn_click(object sender, eventargs e) { bool run = false; if (schedule_chk.checked == true) { run = true; if (recur_txt.text != "" || recur_txt.text != "0") { while (run == true) { if ((convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text))) { datetime dt1; datetime dt2; timespan ts = new timespan(convert.toint16(recur_txt.text),0,0,0); dt1 = convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")); dt2 = convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text); if (dt1 >= dt2) { execute_command(); location_alert_timer.interval = convert.toint32((convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text + ":00.000").add(ts) - datetime.now).totalmilliseconds); location_alert_timer.start(); timeractive = true; timerhold(); } else { location_alert_timer.interval = convert.toint32((convert.todatetime(datetime.now.tostring("mm/dd/yyyy") + " " + recur_time_txt.text + ":00.000") - datetime.now).totalmilliseconds); singleuse = true; location_alert_timer.start(); timeractive = true; timerhold(); messagebox.show(datetime.now.tostring()); messagebox.show(datetime.now.add(ts).tostring()); messagebox.show((convert.todatetime(datetime.now.add(ts).tostring()) - datetime.now).totalmilliseconds.tostring()); location_alert_timer.interval = convert.toint32((convert.todatetime(datetime.now.add(ts).tostring()) - datetime.now).totalmilliseconds); location_alert_timer.start(); timeractive = true; timerhold(); } } else { location_alert_timer.stop(); run = false; } } } } } public void timerhold() { while (timeractive) { application.doevents(); } } void location_alert_timer_tick(object sender, eventargs e) { if (singleuse) { if (uses < 1) { if (convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text)) { uses++; //doitonce(); execute_command(); } else { timeractive = false; } } else { singleuse = false; timeractive = false; location_alert_timer.stop(); } } else { if (convert.todatetime(datetime.now.tostring("mm/dd/yyyy hh:mm")) <= convert.todatetime(end_date.value.tostring("mm/dd/yyyy") + " " + recur_time_txt.text)) { //doit(); execute_command(); } else { timeractive = false; } } }

what using example?

a-simple-scheduler-in-csharp

c# .net winforms timer .net-3.5

No comments:

Post a Comment