C# windows service handler
To get extended error information, call GetLastError. The ServiceMain function of a new service should immediately call the RegisterServiceCtrlHandler function to register a control handler function with the control dispatcher. This enables the control dispatcher to invoke the specified function when it receives control requests for this service. For a list of possible control codes, see Handler. The threads of the calling process can use the service status handle returned by this function to identify the service in subsequent calls to the SetServiceStatus function.
The RegisterServiceCtrlHandler function must be called before the first SetServiceStatus call because RegisterServiceCtrlHandler returns a service status handle for the caller to use so that no other service can inadvertently set this service status.
In addition, the control handler must be in place to receive control requests by the time the service specifies the controls it accepts through the SetServiceStatus function. When the control handler function is invoked with a control request, the service must call SetServiceStatus to report status to the service control manager only if the service status has changed, such as when the service is processing stop or shutdown controls.
If the service status has not changed, the service should not report status to the service control manager. For an example, see Writing a ServiceMain Function. The winsvc. If DateTime. AddDays 1. End If. If mode. AppSettings "IntervalMinutes". AddMinutes intervalMinutes. Catch ex As Exception. Using serviceController As New System. End Using. End Try. WriteLine String. End Class. Adding an Installer to the Windows Service.
Once the Windows Service is ready to go we need to add the Installer class to our Windows Service as without it, the Windows Service will not install.
Following are the steps to add Installer class. Right Click the Service1. Once the Design View is show, you need to right click and then select Add Installer in the context menu. The above action will add an Installer class named ProjectInstaller. Now you need to open the ProjectInstaller.
Designer class and look for InitializeComponent Method. In this method we will modify the ServiceName of the Windows Service and also set its StartType to Automatic, so that along with the computer the Windows Service will start automatically.
ServiceProcessInstaller ;. ServiceInstaller ;. AddRange new System. Private Sub InitializeComponent. AddRange New System. ServiceProcessInstaller1, Me.
Making the Windows Service Automatically start after Installation. After the installation one has to start the Windows Service manually through the Services section of My Computer Management. We can start the Windows Service automatically after installation by making use of the AfterInstall event handler which triggers immediately after Windows Service is installed.
You will need to open the ProjectInstaller class and override the AfterInstall event handler and add the code to start the Windows Service. OnAfterInstall savedState ;. ServiceController serviceInstaller1. Start ;. Public Class ProjectInstaller. Public Sub New. OnAfterInstall savedState. ServiceController ServiceInstaller1. Installing the Windows Service using InstallUtil. Once all the processes are complete, we can now build the Windows Service. Note : Once the Windows Service is ready for deployment, it is recommended to make use of the Release version of the EXE file instead of the Debug version.
Now copy and build the path in a Notepad Text file. Note : I would recommend to build the command in a Notepad and save it somewhere so that you can use it multiple times. InstallUtil Syntax. Note : I am making use of Visual Studio , and hence you need to use the appropriate version installed on your computer.
And make sure you are logged in as Administrator. Without Administrator rights it would not allow you to install the Windows Service. In the command prompt window, copy the InstallUtil command from Notepad and right click in the Command Prompt and click Paste and then press Enter key.
Now the Installer will ask for Logon permissions to run the Windows Service and hence you will need to add Windows Username and Password of user who has appropriate permission. Note : Username must include Domain Name or the Computer name. After successful installation you will see the following message.
You can find the Windows Service in the Services window. If the operating system is rebooting, the time limit is specified in the WaitToKillServiceTimeout value in milliseconds of the following registry key:. A service should not attempt to increase the time limit by modifying this value.
Customers require fast shutdown of the operating system. For example, if a computer running on UPS power cannot complete shutdown before the UPS runs out of power, data can be lost. Therefore, services should complete their cleanup tasks as quickly as possible. It is a good practice to minimize unsaved data by saving data on a regular basis, keeping track of the data that is saved to disk, and only saving your unsaved data on shutdown.
Because the computer is being shut down, do not spend time releasing allocated memory or other system resources. If you need to notify a server that you are exiting, minimize the time spent waiting for a reply, because network problems could delay the shutdown of your service.
Note that during service shutdown, by default, the SCM does not take dependencies into consideration. Therefore, a service may fail because another service it depends on has already stopped. To set the shutdown order of services manually, create a multistring registry value that contains the service names in the order in which they should be shut down and assign it to the Control key's PreshutdownOrder value, as follows:. To set the shutdown order of dependent services from your application, use the SetProcessShutdownParameters function.
The SCM uses this function to give its handler 0x1E0 priority. Writing a Control Handler Function. Skip to main content.
0コメント