Windows services code




















Parse System. AppSettings[ "ScheduledTime" ] ;. AddDays 1 ;. ToInt32 ConfigurationManager. AppSettings[ "IntervalMinutes" ] ;. AddMinutes intervalMinutes ;. Subtract DateTime. Now ;. Days, timeSpan. Hours, timeSpan. Minutes, timeSpan. Seconds ;. ToInt32 timeSpan. TotalMilliseconds ;. Change dueTime, Timeout. Infinite ;. StackTrace ;. ServiceController "SimpleService". Stop ;. WriteLine string. Format text, DateTime. Close ;. Public Class Service1. End Sub. Protected Overrides Sub OnStop.

Private Schedular As Timer. Public Sub ScheduleService. AppSettings "Mode". WriteToFile Convert. AppSettings "ScheduledTime". 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.

The process handle can be the current process or a process that almost always runs, explorer. Next, we call DuplicateTokenEx to duplicate the token. Next, we create the environment for the new process to be created by calling CreateEnvironmentBlock. Now we are ready to create the new process, invoking it from the service and yet creating it under the active user's account. We do that by calling CreateProcessAsUser. Before termination of our application, the cleanup includes calling CloseHandle and DestroyEnvironmentBlock.

After building a skeleton of a service which invokes a process to run in the user's session, the big challenge is to keep track and address a wide range of scenarios such as:.

Copy Code. Diagnostics; using System. IO; using System. Linq; using System. ServiceProcess; using System. Text; using System. Tasks; using System. Now ; timer.

Step 9. Rebuild your application. Right-click on your project or solution and select Rebuild. Syntax InstallUtil. Type services. You may notice that the Windows service is running. Check Windows Service Output The service will create a text file with the following text in it. The log folder will be created in your bin folder.

Uninstalling a Windows Service If you want to uninstall your service, fire the below command. I hope, you found this tutorial easy to follow and understand.



0コメント

  • 1000 / 1000