ServiceController.ExecuteCommand(Int32) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menjalankan perintah kustom pada layanan.
public:
void ExecuteCommand(int command);
public void ExecuteCommand(int command);
member this.ExecuteCommand : int -> unit
Public Sub ExecuteCommand (command As Integer)
Parameter
- command
- Int32
Bendera perintah yang ditentukan aplikasi yang menunjukkan perintah kustom mana yang akan dijalankan. Nilai harus antara 128 dan 256, inklusif.
Pengecualian
Terjadi kesalahan saat mengakses API sistem.
Layanan tidak ditemukan.
Contoh
Contoh kode berikut menunjukkan penggunaan ServiceController.ExecuteCommand(Int32) metode untuk menjalankan perintah kustom dalam SimpleService contoh layanan.
using System;
using System.ServiceProcess;
namespace test_exec_cmnd
{
class Program
{
private enum SimpleServiceCustomCommands { StopWorker = 128, RestartWorker, CheckWorker };
static void Main(string[] args)
{
ServiceController myService = new ServiceController("SimpleService");
myService.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
myService.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
myService.ExecuteCommand((int)SimpleServiceCustomCommands.CheckWorker);
}
}
}
Imports System.ServiceProcess
Class Program
Private Enum SimpleServiceCustomCommands
StopWorker = 128
RestartWorker
CheckWorker '
End Enum 'SimpleServiceCustomCommands
Shared Sub Main(ByVal args() As String)
Dim myService As New ServiceController("SimpleService")
myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.StopWorker))
myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.RestartWorker))
myService.ExecuteCommand(Fix(SimpleServiceCustomCommands.CheckWorker))
End Sub
End Class
Keterangan
Saat Anda memanggil ExecuteCommand, status layanan tidak berubah. Jika layanan dimulai, statusnya tetap Running. Jika layanan dihentikan, statusnya tetap Stopped, dan sebagainya. Untuk memproses perintah kustom, layanan harus mengambil alih OnCustomCommand metode dan menyediakan handler untuk perintah yang diidentifikasi oleh command parameter .