C # Windows Service Program

Source: Internet
Author: User

Windows Forms applications run after a user logs on. Especially for the server this multi-user system, although set up boot, but in the process of running the program, the user is logged off, the program is turned off. Unless someone logs on again or the server restarts.


If you want the program to run on the server, it is best to write the program as a Windows service program. This program will start with the automatic start of the system, automatically shut down and shut down, do not require the user to log in directly, the boot can be started.

Note that the Windows Service program does not have an interface, so define the issues that occur during the log file save run. If the timer is used, it is the system timer.

The following example shows the He Jian Windows service program, taking VS2013 as an example:

1. Setting up a Windows service

See what vs has generated.


2. Rename Service1, which is the name of the Windows service that will run later, change to SQLServerSyncService.cs

Open SQLServerSyncService.cs, add background code

namespace sqlserversyncservice{public partial class Sqlserversyncservice:servicebase {private System.time Rs.        Timer timer1;//System timer public int No = 0;                    Public Sqlserversyncservice () {InitializeComponent (); } protected void Timer1_Tick (object source, Elapsedeventargs e) {timer1.            Stop ();            int Timek = 0;            Operatordatatable test = new operatordatatable (); if (TEST.SW! = null) {Test.sw.WriteLine ("No." + (No + 1).            ToString () + "Secondary synchronization database error message:");            } DateTime time1 = DateTime.Now; try {if (test. Readdatafromdatabase ()) {test.                    Writedatatodatabase ();                    BOOL flag = FALSE; DO {flag = test.                        Deletesyncdata ();                    Thread.Sleep (1000);       } while (!flag);         }} finally {test.                Disposeallresource (); Test.                CloseServer (); Test.                Savelogfile ();                DateTime time2 = DateTime.Now; Timek = (time2-time1). Hours * 3600 + (TIME2-TIME1). Minutes * + (TIME2-TIME1).            Seconds;            } no++; Timer1.        Start ();            } protected override void OnStart (string[] args) {timer1 = new System.Timers.Timer (); Timer1.            Elapsed + = new Elapsedeventhandler (Timer1_Tick); String timestr = Xmlconfig.getvalue ("AppSettings", "Add", "Syncinterval", "value");//synchronization interval timer1. Interval = (int) (float.            Parse (TIMESTR) * 60 * 1000); Timer1. AutoReset = true;//Whether the setting is to execute (false) or always execute (true) timer1.            Enabled = true;//Whether to execute System.Timers.Timer.Elapsed event} protected override void OnContinue () {//Service recovery Timer1.        Enabled = true; } protecteD override void OnStop () {timer1.        Enabled = false; }    }}


3. Add the installer, mainly to set up the Windows service program, you can also write your own code implementation, but why.

Right-click

Two components will appear

Right-ServiceInsraller1, select Properties, and change the value of ServiceName to Sqlserversyncservice. (this name is the name of the service later)

Right-ServiceProcessInsraller1, select Properties, and change the account value to LocalSystem.

Build the solution, and finally copy the generated EXE to the easy-to-remember directory. The secondary directory is the path to the service that is generated by subsequent installations.


4. Install the service program. Open the VS Developer command prompt tool and note that you want to run as an administrator, or you will get an error:Windows Service Installation exception: System.Security.SecurityException: Source not found, but some or all of the event logs could not be searched. Inaccessible logs: Security

Enter command InstallUtil.exe C:\Server\SQLServerSyncService.exe, enter


5. The service program is installed, and it's ready to start.

In the above VS Developer debugging tool or Doc input services.msc, enter the popup Service dialog box, find our service program, start on it




C # Windows Service Program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.