このコードにぶつかっただけで、理解できません。経過したコードを AutoReset true で再実行する代わりに、この設計を使用する理由はありますか?
private readonly Timer Timer = new Timer();
protected override void OnStart(string[] args)
{
Logger.InfoFormat("Starting {0}.", ServiceName);
try
{
// If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.
Timer.AutoReset = false;
Timer.Elapsed += Timer_Elapsed;
Timer.Interval = Settings.Default.ScriptingStatusLifeTime;
Timer.Start();
}
catch (Exception exception)
{
Logger.ErrorFormat("An error has occurred while starting {0}.", ServiceName);
Logger.Error(exception);
throw;
}
}
/// <summary>
/// Whenever the Schedule Service time elapses - go to the ScriptingStatus table
/// and delete everything created earlier than 1 hour ago (by default, read from ScriptingStatusLifeTime)
/// </summary>
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
// ScriptingStatusLifeTime defaults to 60 minutes.
DateTime deleteUntil = DateTime.Now.AddMilliseconds(Settings.Default.ScriptingStatusLifeTime * -1);
Logger.InfoFormat("Clearing all ScriptingStatus entries with ControlDate before: {0}.", deleteUntil);
RemoteActivator.Create<RemoteScriptingStatus>().DeleteUntil(deleteUntil);
}
catch (Exception exception)
{
Logger.Error(exception);
}
finally
{
Timer.Start();
}
}
さらに、このコードでメモリリークを探しています。
この投稿を読みました:自動リセットが false に設定されている場合、タイマーは自動的に破棄されますか? これは、Timer オブジェクトを適切に破棄する必要があることを暗示しているようです。現在のファイルに Dispose の呼び出しがありません。この Timer_Elapsed イベントもリークを引き起こしているのでしょうか?