Export (0) Print
Expand All

ServiceBase.OnStart Method

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.

[Visual Basic]
Protected Overridable Sub OnStart( _
   ByVal args() As String _
)
[C#]
protected virtual void OnStart(
 string[] args
);
[C++]
protected: virtual void OnStart(
 String* args __gc[]
);
[JScript]
protected function OnStart(
   args : String[]
);

Parameters

args
Data passed by the start command.

Remarks

Use OnStart to specify the processing that occurs when the service receives a Start command. OnStart is the method in which you specify the behavior of the service. OnStart can take arguments as a way to pass data, but this usage is rare.

CAUTION   Do not use the constructor to perform processing that should be in 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. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. 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.

Services can be set to start when the computer reboots by setting the StartType on the service's installer. In such a situation, OnStart would be called at system startup.

OnStart is expected to be overridden in the derived class. For the service to be useful, OnStart and OnStop should both be implemented in your service class.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

ServiceBase Class | ServiceBase Members | System.ServiceProcess Namespace | OnContinue | OnPause | OnPowerEvent | OnShutdown | OnStop | OnCustomCommand

Show:
© 2014 Microsoft