ServiceBase.OnStop メソッド
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)
構文
解説OnStop を使用して、サービスが Stop コマンドを受信したときに発生する処理を指定します。
SCM では、Stop コマンドを受け取ると、CanStop の値を使用して、サービスが Stop コマンドを受け入れるかどうかを確認します。CanStop が true の場合、Stop コマンドはサービスに渡され、OnStop メソッドが定義されている場合は呼び出されます。OnStop がサービスに実装されていない場合は、SCM が Stop コマンドを処理します。
CanStop が false の場合、Stop コマンドは SCM で無視されます。Stop コマンドはサービスには渡されません。Stop コマンドは終了し、例外がスローされます。
OnStop は、派生クラスでオーバーライドされます。サービスを有効に使用するには、OnStart と OnStop の両方をサービス クラスに実装する必要があります。
使用例ServiceBase から派生したサービス クラスに対して OnStop メソッドを実装する例を次に示します。このコード例は、ServiceBase クラスのトピックで取り上げているコード例の一部分です。
' Stop this service. Protected Overrides Sub OnStop() ' New in .NET Framework version 2.0. Me.RequestAdditionalTime(4000) ' Signal the worker thread to exit. If Not (workerThread Is Nothing) AndAlso workerThread.IsAlive Then #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnStop", DateTime.Now.ToLongTimeString() + _ " - Stopping the service worker thread.") #End If pause.Reset() Thread.Sleep(5000) workerThread.Abort() End If If Not (workerThread Is Nothing) Then #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnStop", DateTime.Now.ToLongTimeString() + _ " - OnStop Worker thread state = " + workerThread.ThreadState.ToString()) #End If End If ' Indicate a successful exit. Me.ExitCode = 0 End Sub 'OnStop
// Stop this service. protected override void OnStop() { // New in .NET Framework version 2.0. this.RequestAdditionalTime(4000); // Signal the worker thread to exit. if ((workerThread != null) && (workerThread.IsAlive)) { #if LOGEVENTS EventLog.WriteEntry("SimpleService.OnStop", DateTime.Now.ToLongTimeString() + " - Stopping the service worker thread."); #endif pause.Reset(); Thread.Sleep(5000); workerThread.Abort(); } if (workerThread != null) { #if LOGEVENTS EventLog.WriteEntry("SimpleService.OnStop", DateTime.Now.ToLongTimeString() + " - OnStop Worker thread state = " + workerThread.ThreadState.ToString()); #endif } // Indicate a successful exit. this.ExitCode = 0; }
// Stop this service. protected: virtual void OnStop() override { // Signal the worker thread to exit. if ( (workerThread != nullptr) && (workerThread->IsAlive) ) { Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Stopping the service worker thread.", "OnStop" ); workerThread->Abort(); // Wait up to 500 milliseconds for the thread to terminate. workerThread->Join( 500 ); } if ( workerThread != nullptr ) { Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Worker thread state = " + workerThread->ThreadState.ToString(), "OnStop" ); } }
.NET Framework のセキュリティ
プラットフォームWindows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照ServiceBase クラス
ServiceBase メンバ
System.ServiceProcess 名前空間
CanStop
OnContinue
OnPowerEvent
OnPause
OnShutdown
OnStart
OnCustomCommand
Weblioに収録されているすべての辞書からServiceBase.OnStop メソッドを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からServiceBase.OnStop メソッド
を検索
- ServiceBase.OnStop メソッドのページへのリンク