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.

 virtual void OnPause();
protected virtual void OnPause ();
abstract member OnPause : unit -> unit
override this.OnPause : unit -> unit
Protected Overridable Sub OnPause ()


使用OnPause來指定服務收到 Pause 命令時所發生的處理。Use OnPause to specify the processing that occurs when the service receives a Pause command. OnPauseCanPauseAndContinue屬性為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.

將暫停要求傳送至服務可節省系統資源,因為 Pause 不需要釋放所有系統資源。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. 如果您定義暫停以釋放所有系統資源,它的行為就像是 Stop 命令。If you define Pause to release all system resources, it behaves like a Stop command.

CanPauseAndContinue設定true為,並OnPauseOnContinue寫和,以指定當 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.

如果CanPauseAndContinuefalse,則 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. Pause SCM 中,當為Continue CanPauseAndContinue false時,會停用和控制項。In the SCM, the Pause and Continue controls are disabled when CanPauseAndContinue is false.