Assembly: System.ServiceProcess (in system.serviceprocess.dll)
'Declaration Protected Overridable Sub OnStart ( _ args As String() _ ) 'Usage Dim args As String() Me.OnStart(args)
Data passed by the start command.
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.
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.
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.
Process initialization arguments for the service in the OnStart method, not in the Main method. 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. 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>). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string imagePathArgs = Environment.GetCommandLineArgs();.
The following example shows an implementation of the OnStart method for a service class derived from ServiceBase. This code example is part of a larger example provided for the ServiceBase class.
' Start the service. Protected Overrides Sub OnStart(ByVal args() As String) Dim handle As IntPtr = Me.ServiceHandle myServiceStatus.currentState = Fix(State.SERVICE_START_PENDING) SetServiceStatus(handle, myServiceStatus) ' Start a separate thread that does the actual work. If workerThread Is Nothing OrElse (workerThread.ThreadState And System.Threading.ThreadState.Unstarted Or System.Threading.ThreadState.Stopped) <> 0 Then #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnStart", DateTime.Now.ToLongTimeString() + _ " - Starting the service worker thread.") #End If workerThread = New Thread(New ThreadStart(AddressOf ServiceWorkerMethod)) workerThread.Start() End If If Not (workerThread Is Nothing) Then #If LOGEVENTS Then System.Diagnostics.EventLog.WriteEntry("SimpleService.OnStart", DateTime.Now.ToLongTimeString() + _ " - Worker thread state = " + workerThread.ThreadState.ToString()) #End If End If myServiceStatus.currentState = Fix(State.SERVICE_RUNNING) SetServiceStatus(handle, myServiceStatus) End Sub 'OnStart