繁体   English   中英

不是将EventLog使用Designer生成的代码放置在Windows Service应用程序中就可以了吗?

[英]Not EventLog dispose in Windows Service app using the Designer generated code is fine?

在使用Windows Service VS2013项目模板创建项目时,我注意到从工具框中添加EventLog组件后,名为“组件设计器生成的代码”的#region将填充以下代码:

    private void InitializeComponent()
    {
        this.eventLog1 = new System.Diagnostics.EventLog();
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
        // 
        // Service1
        // 
        this.ServiceName = "Service1";
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

    }

我的问题是:应该添加Designer生成类似于以下内容的行:

components.Add(this.eventLog1);

下线后

this.eventLog1 = new System.Diagnostics.EventLog();

存在的方法类似于:

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

并且System.Diagnostics.EventLog实现了IDisposable接口,使我认为这样的行应包含在Designer中,并生成代码。 我知道这没什么大不了的,只是在极少数情况下(例如,您在Main方法中使用ServicesBase派生的实例打脏了),缺少该行可能会影响应用程序的性能,但对我而言仍然没有意义像这样的Disposal方法实现,而不添加以下行: components.Add(this.eventLog1); 有什么想法吗?

在我看来,这似乎是个错误。 EventLog类忘记实现适当的构造函数,该构造函数接受容器引用。 不需要这样做,基本示例是BackgroundWorker和OpenFileDialog,它们没有任何可处理的内容。

但是可以,EventLog可以。 不确定是否有人会注意到这一点,事件日志通常在程序的整个生命周期内进行,而EventLog实例通常存储在静态变量中。 修复起来很容易:

using System;
using System.ComponentModel;
using System.Diagnostics;

public class MyEventLog : EventLog {
    public MyEventLog() { }
    public MyEventLog(IContainer container) : this() { container.Add(this); }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM