我遇到问题让代码在服务上运行系统计时器,实际上没有计时器IS触发,但是它下面的代码不会运行。
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service Started");
//ad 1: handle Elapsed event
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
//ad 2: set interval to 1 minute (= 60,000 milliseconds)
timer.Interval = 60000;
timer.AutoReset = true;
//ad 3: enabling the timer
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
EventLog.WriteEntry("TIMER FIRED");
string[] filepaths = Directory.GetFiles(path, Filetype);
string result;
// Process Each String/File In Directory
foreach (string s in filepaths)
{
for (int i = 0; i < filepaths.Length; i++)
{
//Result Returns Video Name
result = Path.GetFileNameWithoutExtension(filepaths[i]);
PreformTranslation(filepaths[i], outputPath + result);
if (File.Exists(path + result))
{
MoveVideoFiles(path + result, outputPath + result);
}
}
}
}
所以我从事件查看器看到它实际上每分钟发送“计时器已启动”,但不会先运行下面的任何代码。如果没有定时器,代码就会运行,所以初始我只是尝试调用方法,当这不起作用时,我只是将代码放在事件处理程序中,希望这会改变。当我诉诸希望时,我想我已经迷失了。