私はこのコードに遭遇しました:IDisposableを実装しているWindowsサービス - 悪い習慣ですか?
public class ServiceLauncher2 : ServiceBase, IDisposable
そして、この:
/// <summary>
/// Disposes the controllers
/// </summary>
// This is declared new as opposed to override because the base class has to be able to
// call its own Dispose(bool) method and not this one. We could just as easily name
// this method something different, but keeping it Dispose is just as valid.
public new void Dispose()
{
foreach (var mgr in _threadManagers)
mgr.Dispose();
base.Dispose();
}
私が前にWindowsサービスの実装でこれを見たことがありません。通常、OnStop/OnStartだけがオーバーライドされます。これは悪い習慣ですか?
ある
Component
あるようIDisposable
として宣言ServiceLauncher2
は、冗長ですコンソール経由で実行するコード(interative)ですが、展開するとサービスです。 –OK、コンソールモードでは、OnStopが呼び出されないのでDisposeメソッドをそのまま残します。 –
'ServiceBase'では、プロセス全体が終了したときにデストラクタが呼び出されるため、管理されていないすべてのリソースが解放されます。インスタンス化され、複数回破壊されるライブラリクラスを扱っているわけではなく、メモリリークやリソースの枯渇の原因となります。 – ajeh