ServiceBase.OnPause 方法
定义
在派生类中实现时,该方法于“服务控制管理器”(SCM) 将“暂停”命令发送到服务时执行。When implemented in a derived class, executes when a Pause command is sent to the service by the Service Control Manager (SCM). 指定要在服务暂停时采取的操作。Specifies actions to take when a service pauses.
protected:
virtual void OnPause();
protected virtual void OnPause ();
abstract member OnPause : unit -> unit
override this.OnPause : unit -> unit
Protected Overridable Sub OnPause ()
注解
用于 OnPause 指定在服务接收暂停命令时发生的处理。Use OnPause to specify the processing that occurs when the service receives a Pause command. OnPause 当属性为时,应重写 CanPauseAndContinue true 。OnPause is expected to be overridden when the CanPauseAndContinue property is true.
通过服务控制台或以编程) 方式 (继续暂停的服务时, OnContinue 处理将运行,并且服务将再次变为活动状态。When you continue a paused service (either through the Services console or programmatically), the OnContinue processing is run, and the service becomes active again.
Pause 命令仅允许应用程序响应特定事件。The Pause command only allows your application to react to a specific event. OnPause 对未定义的服务执行任何操作。OnPause does nothing to the service that you do not define it to do.
向服务发送暂停请求可能会保留系统资源,因为暂停无需释放所有系统资源。Sending a Pause request to the service can conserve system resources because Pause need not release all system resources. 例如,如果线程已由进程打开,则暂停服务而不是停止它可以使线程保持打开状态,避免在服务继续时需要重新分配它们。For example, if threads have been opened by the process, pausing a service rather than stopping it can allow the threads to remain open, obviating the need to reallocate them when the service continues. 如果将 Pause 定义为释放所有系统资源,它的行为类似于停止命令。If you define Pause to release all system resources, it behaves like a Stop command.
设置 CanPauseAndContinue 为 true ,并重写 OnPause 和, OnContinue 以指定当 SCM 向服务传递暂停或继续请求时应发生的处理。Set CanPauseAndContinue to true, and override OnPause and OnContinue to specify the processing that should occur when the SCM passes a Pause or Continue request to your service. OnContinue 应实现以撤消中的处理 OnPause 。OnContinue should be implemented to undo the processing in OnPause.
如果 CanPauseAndContinue 为 false ,则 SCM 将不会向服务传递暂停或继续请求,因此 OnPause ,即使实现了,也不会 OnContinue 调用和方法。If CanPauseAndContinue is false, the SCM will not pass Pause or Continue requests to the service, so the OnPause and OnContinue methods will not be called even if implemented. 在 SCM 中,在 Pause Continue 为时禁用和控件 CanPauseAndContinue false 。In the SCM, the Pause and Continue controls are disabled when CanPauseAndContinue is false.