1. Create a Windows Service project
2. Right-click on the Service1.cs to see the code for writing operational Logic code
3. OnStart in code to perform service events
Public partial class Service1:servicebase {string LogFilePath = ""; Loghelper Loghelper; WENDYWUBLL BLL = new WENDYWUBLL (); Public Service1 () {LogFilePath = configurationmanager.appsettings["Syncfeefodatalog"]; Loghelper = new Loghelper (LogFilePath); InitializeComponent (); AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_unhandledexception ); } void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) {try {Exception ex = e.exceptionobject as Exception; Loghelper.writeline (The global exception from "Monitoronserver". "+ Ex. Message + "Details are as follows:" + Environment.NewLine + "[innerexception]" + ex. InnerException + Environment.NewLine + "[Source]" + ex. Source + environmenT.newline + "[TargetSite]" + ex. TargetSite + Environment.NewLine + "[StackTrace]" + ex. StackTrace); } catch {}} private delegate void Newtaskdelegate (); protected override void OnStart (string[] args) {EventLog.WriteEntry ("Synchronous FEEFO Data Service startup"); Description of the source in the application event in the System Event Viewer loghelper.writeline ("service startup"); Custom log Loghelper.writeline ("service version 1.1"); Newtaskdelegate task = BLL. Intdata; Use the BeginInvoke method to call the NewTask method asynchronously and pass in 2000 as the NewTask parameter IAsyncResult asyncResult = task. BeginInvoke (null, NULL); System.Timers.Timer t = new System.Timers.Timer (); T.interval = 1000; T.elapsed+=new System.Timers.ElapsedEventHandler (CHKSRV);///Time of arrival execution T.autoreset = true;//setting executes once (false) or always executes ( true) t.enabled = true;//Whether the system is executed. Timers.Timer.ElapseEventHander event BLL. SetServiceStatus ("Wendywuautoservice", "RUnning "); private void Chksrv (object sender, System.Timers.ElapsedEventArgs e) {int inthour = E.signalti Me. Hour; int intminute = E.signaltime.minute; int intsecond = E.signaltime.second; if ((Intminute = = XX && Intsecond = = 00) | | (Intminute = = && Intsecond = = 00)) Set the timer to execute {try {loghelper.writeline ("open") at eight o ' clock per day Sync data from Feefo "); System.Timers.Timer TT = (System.Timers.Timer) sender; Bll. Syncdatafromfeefo (); Loghelper.writeline ("Sync data from Feefo complete"); } catch (Exception ex) {Loghelper.writeline (ex. Message); }} else if ((Intminute = = && Intsecond = = 00) | | (Intminute = = && Intsecond = = 00) | | (Intminute = = && Intsecond = = 00) | | (Intminute = = &&Amp Intsecond = = 00) | | (Intminute = = && Intsecond = = 00)) {try {loghelper.writeline ("Automatic mail delivery service startup" + DateTime.Now.ToString ()); Bll. SendEmail (); Loghelper.writeline ("Mail send Complete" + DateTime.Now.ToString ()); } catch (Exception ex) {Loghelper.writeline (ex. Message); }}} protected override void OnStop () {loghelper.writeline ("service Stop"); EventLog.WriteEntry ("Synchronous FEEFO Data Service Stop"); Bll. SetServiceStatus ("Wendywuautoservice", "Stopped"); } }
Install the service configuration
1. Open the Service1.cs View interface
2. Right-click inside the view to add the installer
3.
The ProjectInstaller.cs file is added to the project, and the view in the file automatically adds two components
ServiceProcessInstaller1
ServiceInstaller1
4. Select the ServiceProcessInstaller1 component, view the properties, set account to LocalSystem
5. Select the ServiceInstaller1 component to view the properties
Sets the value of the servicename that represents the name in the system service
Set StartType, if manual is started manually, by default stop, if automatic for auto start
Set description, Add service description
Installation Services
1. Click Start, enter CMD in the run, get command prompt
Win7 needs to be started as an administrator or cannot be installed
2.
Enter CD C:\Windows\Microsoft.NET\Framework\v4.0.30319 carriage return
To switch the current directory, it is important to note that in the C:\Windows\Microsoft.NET\Framework directory there are many similar versions, specifically to which directory to see the project's operating environment, for example, the result is. NET framework2.0 need to input CD C: \ windows\microsoft.net\framework\v2.0.50727
3.
Input InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe Carriage return
Description: E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe represents the exe file location for project generation
4. Open the service and you will see the services already installed.
Uninstall Service
Uninstall is simple, open cmd, direct input SC delete winservicetest can
C # Creating Windows Services and Installation-plots