我想在預約時間前每3或5分鐘發一封電子郵件,但由於某些原因代碼每秒都會觸發,我只希望它在計時器到達threashold之前觸發分配的時間,但我無法實現。並且還會使調試更容易。僅當後臺計時器達到閾值而不是每分鐘時才希望觸發代碼?
protected void Page_Load(object sender, EventArgs e) {
Timer timer = new Timer();
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
timer.Elapsed += timer_Elapsed;
timer.Interval = (1000) * (2);
timer.Enabled = true;
timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e) {
if (!worker.IsBusy)
worker.RunWorkerAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e) {
//whatever You want the background thread to do...
doReminders(3);
}
/// <summary>
/// Does the reminders.
/// sends out reminders based on the amount of minuties before a meeting
/// </summary>
protected void doReminders(int reminder) {
try {
List<ApertureDal.Appointment> _appointments = _dal.GetAppointmentsByReminderLength(reminder);
_appointments.ForEach(x => {
_dal.sendAppointmentEmails(x.ID, x.emailAddress, x.TimeCode, x.emailAddress, new Guid(Constants.calenderEmail), x.CustomerFirstName, x.CustomerLastName, x.managerName, x.preferedContactNumber, x.emailAddress, x.Start, x.End, x.managerId);
});
} catch (Exception ex) {}
}
編輯以顯示GetAppointments功能
/// <summary>
/// Gets the appointments.
/// </summary>
/// <param name="reminderLength">Length of the reminder.</param>
/// <returns></returns>
public List<Appointment> GetAppointmentsByReminderLength(int reminderLength)
{
List<Appointment> list = new List<Appointment>();
try
{
var q = from a in apertureNetEntities.Appointments //.Where(a => a.Start.Value.AddMinutes(-reminderLength) <= DateTime.Now)
select a;
list = q.ToList();
}
catch (Exception ex)
{
string inner = string.Empty;
if (ex.InnerException != null)
{
inner = ex.InnerException.ToString();
}
logger.Error("Error in List<Appointment> function GetAppointmentsByReminderLength " + ex.ToString() + " " + inner);
return null;
}
return list;
}
編輯 目前它向我發送電子郵件98,即使我的分貝他們是唯一一個我測試記錄。
我認爲問題在於_dal.GetAppointmentsByReminderLength(提醒),可能是不是很快的returnung約會。 – dryman
@dryman我對問題進行了編輯以顯示GetAppointmentsByReminderLength – rogue39nin
GetAppointmentsByReminderLength返回所有約會,無論它們是否到期,因爲LINQ Where在哪裏被評論。很明顯,它會每隔2秒發送一次所有約會。但是想要的行爲是什麼?如果它應該每隔3分鐘發送一次所有點數,請更改timer.interval,如果你想每2秒檢查一次,只發送預約,你應該取消LING Where的註釋並用它來過濾約會發送。 – dryman