Tôi có một dịch vụ windows đang chạy, bên trong này tôi muốn chạy một hàm sau đó mỗi phút. Tôi đã tìm thấy một số mã nhưng nó dường như không hoạt động? Tôi có một logger và nó dường như không đi vào chức năng timer_Elapsed bao giờ?làm thế nào để có một chức năng chạy bên trong một dịch vụ mỗi 10 phút?
protected override void OnStart(string[] args)
{
// SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
// test.Import();
log.Info("Info - Service Started");
_timer = new Timer(10 * 60 * 1000); // every 10 minutes??
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
log.Info("Info - Check time");
DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48);
if (_lastRun < startAt && DateTime.Now >= startAt)
{
// stop the timer
_timer.Stop();
try
{
log.Info("Info - Import");
SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
test.Import();
}
catch (Exception ex) {
log.Error("This is my error - ", ex);
}
_lastRun = DateTime.Now;
_timer.Start();
}
}
Bạn có cần phải gọi bắt đầu trên Bộ hẹn giờ không? –
xem http://stackoverflow.com/questions/246697/windows-service-and-timer –
ahhh yeeeeeeeee – Beginner