ServiceBase 构造函数

定义

创建 ServiceBase 类的新实例。Creates a new instance of the ServiceBase class.

public:
 ServiceBase();
public ServiceBase ();
Public Sub New ()

注解

请勿创建类的实例 ServiceBaseDo not create an instance of the ServiceBase class. 相反,派生自 ServiceBase 并实例化派生类。Instead, derive from ServiceBase and instantiate your derived class. 在从继承的类的构造函数中实现所需的最小值 ServiceBase 是在 ServiceName 组件上设置。The minimum you need to implement in the constructor for a class inherited from ServiceBase is to set the ServiceName on your component. 构造函数中不特别需要其他处理。No other processing is specifically required in the constructor. 你应在中处理大多数初始化, OnStart 而不是在构造函数中。You should handle most initialization in OnStart rather than in the constructor. 否则,在服务停止后重新启动该服务时,对象将重新初始化。Otherwise, there is no guarantee the objects will be reinitialized when you restart a service after it has been stopped.

如果重写派生类中的构造函数,请在代码中调用基类构造函数。If you override the constructor in your derived class, call the base class constructor in your code.

ServiceBase构造函数将设置 AutoLogtrueThe ServiceBase constructor sets AutoLog to true. 如果你不希望从服务控制管理器 (SCM) 自动记录对服务的调用,请将设置 AutoLogfalseIf you do not want to automatically log calls to the service from the Service Control Manager (SCM), set AutoLog to false.

如果可执行文件包含单个服务,则在从 SCM 中选择 "启动" 时,系统会调用服务的构造函数,如果调用了 Stop,则会运行析构函数。If the executable contains a single service, the system calls the service's constructor when Start is selected from the SCM, and runs the destructor if Stop is called.

如果可执行文件包含多个服务,则对一个服务调用 "启动" 将导致为可执行文件中的所有服务调用构造函数,但只启动指定的服务。If the executable contains multiple services, calling Start on one service causes the constructors to be called for all services in the executable, but only the specified service is started. 所有服务都已停止时,服务的析构函数会一起运行,而不会在每个服务停止时单独运行。Destructors for the services are run together when all services have been stopped, not individually when each service is stopped.

备注

如果重写基类构造函数,则应在派生类的构造函数中显式调用它。If you override the base class constructor, you should explicitly call it in the constructor of your derived class.

适用于

另请参阅