ServiceBase.OnStart(String[]) 方法

定義

在衍生類別中實作時,當服務控制管理員 (SCM) 傳送啟動命令至服務或當作業系統啟動 (自動啟動服務) 時執行。When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). 指定在服務啟動時所要執行的動作。Specifies actions to take when the service starts.

protected:
 virtual void OnStart(cli::array <System::String ^> ^ args);
protected virtual void OnStart (string[] args);
abstract member OnStart : string[] -> unit
override this.OnStart : string[] -> unit
Protected Overridable Sub OnStart (args As String())

參數

args
String[]

啟動命令所傳遞的資料。Data passed by the start command.

備註

使用OnStart來指定服務收到 Start 命令時所發生的處理。Use OnStart to specify the processing that occurs when the service receives a Start command. OnStart這是您用來指定服務行為的方法。OnStart is the method in which you specify the behavior of the service. OnStart可以接受引數做為傳遞資料的方式,但這種用法很罕見。OnStart can take arguments as a way to pass data, but this usage is rare.

警告

請勿使用此函式來執行應該位於中OnStart的處理。Do not use the constructor to perform processing that should be in OnStart. 使用OnStart來處理服務的所有初始化。Use OnStart to handle all initialization of your service. 當應用程式的可執行檔執行時,會呼叫此函式,而不是在服務執行時呼叫。The constructor is called when the application's executable runs, not when the service runs. 可執行檔會OnStart在之前執行。The executable runs before OnStart. 例如,當您繼續時,不會再次呼叫此函式,因為 SCM 已經在記憶體中保存物件。When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. 如果OnStop釋放在此函式中所配置的OnStart資源,而不是在中,則在第二次呼叫服務時,不會再次建立所需的資源。If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called.

服務可以設定為在電腦重新開機時自動啟動,方法是StartType將服務安裝程式上的Automatic設定為。Services can be set to start automatically when the computer reboots by setting the StartType on the service's installer to Automatic. 在這種情況下OnStart ,會在系統啟動時呼叫。In such a situation, OnStart would be called at system startup.

OnStart應該在衍生類別中覆寫。OnStart is expected to be overridden in the derived class. 服務會很有用, OnStart而且OnStop應該同時在您的服務類別中執行。For the service to be useful, OnStart and OnStop should both be implemented in your service class.

OnStart方法中處理服務的初始化引數,而不是在 Main 方法中。Process initialization arguments for the service in the OnStart method, not in the Main method. 您可以在 [ args服務] 主控台中,于服務的 [屬性] 視窗中,手動設定參數陣列中的引數。The arguments in the args parameter array can be set manually in the properties window for the service in the Services console. 不會儲存在主控台中輸入的引數;當服務從 [控制台] 啟動時,它們會一次傳遞至服務。The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. 自動啟動服務時必須存在的引數可以放在服務的登錄機碼的 ImagePath 字串值中(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ <服務名稱 >).Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<service name>). 您可以使用GetCommandLineArgs方法來取得登錄中的引數,例如: string[] imagePathArgs = Environment.GetCommandLineArgs();You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();.

適用於

另請參閱