问题
i have timer in my windows service, but the windows service does not do what it should do.. I want ask you, if i have good code with timer?
Part of my code (updated):
protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(getFileList);
timer.Interval = 10000;
timer.Enabled = true;
timer.AutoReset = false;
}
private void getFileList(object sender, EventArgs e)
{
//Work with xml...
DeleteOldBackupFiles();
}
private void DeleteOldBackupFiles()
{
string[] Oldfiles = Directory.GetFiles(backup);
foreach (string Ofile in Oldfiles)
{
FileInfo fi = new FileInfo(Ofile);
if (fi.LastWriteTime < DateTime.Now.AddMonths(-2))
{
fi.Delete();
}
}
}
After your ideas i edit my code, but result is the same..
protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(getFileList);
timer.Interval = 10000;
timer.Enabled = true;
timer.AutoReset = true;
}
回答1:
You have most likely an error somewhere in your timer making it throw an exception. You will not detect that since System.Timers.Timer
silently ignores all unhandled exceptions.
You'll therefore have to wrap all code with a try/catch block:
private void getFileList(object sender, EventArgs e)
{
try
{
DeleteOldBackupFiles();
}
catch (Exception ex)
{
//log exception or just put a breakpoint here.
}
}
Hence your timer is working, but you are doing something wrong in it.
回答2:
I would change it to this:
protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(getFileList);
timer.Interval = 10000;
timer.AutoReset = false;
timer.Enabled = true;
}
private void getFileList(object sender, EventArgs e)
{
List<string> files = new List<string>();
try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(****);
Setting AutoReset to false causes the Timer to fire just once. In other words, it's like an automatic Stop after the first firing of the Timer. So doing that means you don't need to Stop() the Timer in your getFileList method. This is an important distinction when the Timer interval is small. If you set AutoReset to true and call Stop() at the top of your handler, there's a small chance that your method will get called more than once. Setting AutoReset to false is a more definitive way to get the behaviour you desire.
Calling Start() and setting Enabled to true are redundant, so I removed the Start().
Interval is milliseconds, so I changed that to 10000.
回答3:
Maybe you are having an Exception thrown somewhere in getFileList
, possibly because the service is running with a Current Directory of c:\windows\system32\
.
Add a reference to System.Windows.Forms
then add Environment.CurrentDirectory = System.Windows.Forms.Application.StartupPath;
in your void Main(...)
before your service is created.
as @jgauffin pointed out, you should wrap your getFileList
body in a 'try catch' and log the error to some absolute path like c:\errors.txt
. otherwise you are just guessing whats wrong.
来源:https://stackoverflow.com/questions/18771565/bad-timer-in-windows-service