ServiceBase.OnCustomCommand メソッド
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)
 構文
構文 解説
解説OnCustomCommand を使用すると、サービスの開始、停止、一時中断、継続以外の追加機能を指定できます。
SCM は、サービスが渡された command パラメータをサポートするかどうかを確認するためにカスタム コマンドをチェックしません。SCM はカスタム コマンドを直接サービスに渡します。サービスが command パラメータを認識しない場合は、何も実行されません。
カスタム コマンドは、ServiceController コンポーネントの ExecuteCommand ステートメントで発生します。switch ステートメントまたは if..then 条件を使用して、サービスで定義するカスタム コマンドを処理します。
アプリケーションで定義できるカスタム コマンドの値、または OnCustomCommand で使用できるカスタム コマンドの値は、128 ~ 256 の値だけです。128 未満の整数は、システムで予約される値になります。
AutoLog プロパティが true の場合、カスタム コマンドは、他のすべてのコマンドと同じように、エントリをイベント ログに書き込んで、メソッドの実行が成功したかどうかをレポートします。
 使用例
使用例ServiceBase から派生したサービス クラスに対して OnCustomCommand メソッドを実装する例を次に示します。このコード例は、ServiceBase クラスのトピックで取り上げているコード例の一部分です。
' Handle a custom command. Protected Overrides Sub OnCustomCommand(ByVal command As Integer) #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnCustomCommand", DateTime.Now.ToLongTimeString() + _ " - Custom command received: " + command.ToString()) #End If ' If the custom command is recognized, ' signal the worker thread appropriately. Select Case command Case Fix(SimpleServiceCustomCommands.StopWorker) ' Signal the worker thread to terminate. ' For this custom command, the main service ' continues to run without a worker thread. OnStop() Case Fix(SimpleServiceCustomCommands.RestartWorker) ' Restart the worker thread if necessary. OnStart(Nothing) Case Fix(SimpleServiceCustomCommands.CheckWorker) #If LOGEVENTS Then ' Log the current worker thread state. System.Diagnostics.EventLog.WriteEntry("SimpleService.OnCustomCommand", _ DateTime.Now.ToLongTimeString() + _ " OnCustomCommand - Worker thread state = " + _ workerThread.ThreadState.ToString()) #End If Case Else #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnCustomCommand", _ DateTime.Now.ToLongTimeString()) #End If End Select End Sub 'OnCustomCommand
// Handle a custom command. protected override void OnCustomCommand(int command) { #if LOGEVENTS EventLog.WriteEntry("SimpleService.OnCustomCommand", DateTime.Now.ToLongTimeString() + " - Custom command received: " + command.ToString()); #endif // If the custom command is recognized, // signal the worker thread appropriately. switch (command) { case (int)SimpleServiceCustomCommands.StopWorker: // Signal the worker thread to terminate. // For this custom command, the main service // continues to run without a worker thread. OnStop(); break; case (int)SimpleServiceCustomCommands.RestartWorker: // Restart the worker thread if necessary. OnStart(null); break; case (int)SimpleServiceCustomCommands.CheckWorker: #if LOGEVENTS // Log the current worker thread state. EventLog.WriteEntry("SimpleService.OnCustomCommand" , DateTime.Now.ToLongTimeString() + " OnCustomCommand - Worker thread state = " + workerThread.ThreadState.ToString()); #endif break; default: #if LOGEVENTS EventLog.WriteEntry("SimpleService.OnCustomCommand" , DateTime.Now.ToLongTimeString()); #endif break; } }
// Handle a custom command. protected: virtual void OnCustomCommand( int command ) override { Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Custom command received: " + command, "OnCustomCommand" ); // If the custom command is recognized, // signal the worker thread appropriately. switch ( command ) { case (int)SimpleServiceCustomCommands::StopWorker: // Signal the worker thread to terminate. // For this custom command, the main service // continues to run without a worker thread. OnStop(); break; case (int)SimpleServiceCustomCommands::RestartWorker: // Restart the worker thread if necessary. OnStart( nullptr ); break; case (int)SimpleServiceCustomCommands::CheckWorker: // Log the current worker thread state. Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Worker thread state = " + workerThread->ThreadState.ToString(), "OnCustomCommand" ); break; default: Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Unrecognized custom command ignored!", "OnCustomCommand" ); break; } }
 .NET Framework のセキュリティ
.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 名前空間
OnContinue
OnPause
OnPowerEvent
OnShutdown
OnStart
OnStop
- ServiceBase.OnCustomCommand メソッドのページへのリンク

 
                             
                    


