CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 667

667

Chương 17: Sự hòa hợp với môi trường Windows

EventLog.WriteEntry("ServiceExample active : " + e.SignalTime);

}

protected override void OnStart(string[] args) {

// Lấy chu kỳ ghi sự kiện từ đối số thứ nhất.

// Mặc định là 5000 mili-giây,

// và tối thiểu là 1000 mili-giây.

double interval;

try {

interval = System.Double.Parse(args[0]);

interval = Math.Max(1000, interval);

} catch {

interval = 5000;

}

EventLog.WriteEntry(String.Format("ServiceExample starting. " +

"Writing log entries every {0} milliseconds...", interval));

// Tạo, cấu hình, và khởi động một System.Timers.Timer

// để gọi phương thức WriteLogEntry theo định kỳ.

// Các phương thức Start và Stop của lớp System.Timers.Timer

// giúp thực hiện các chức năng khởi động, tạm dừng, tiếp tục,

// và dừng dịch vụ.

timer = new Timer();

timer.Interval = interval;

timer.AutoReset = true;

timer.Elapsed += new ElapsedEventHandler(WriteLogEntry);

timer.Start();

}

protected override void OnStop() {

EventLog.WriteEntry("ServiceExample stopping...");

timer.Stop();

// Giải phóng tài nguyên hệ thống do Timer sử dụng.