C # windows service programs

Source: Internet
Author: User

C # windows service programs

Windows Forms applications run only after a user logs on. Especially for a multi-user system such as a server, although the system is set to start automatically, but during the process of running the program, the user who runs the program is canceled and the program is closed. Unless someone logs on again or the server restarts.

If you want your program to run on the server all the time, you 'd better write it as a windows service program. In this way, the program will be started with the Automatic startup of the system, and will be closed automatically. You do not need to log on directly and can start the program directly after it is started.

Note that the windows Service Program has no interface, so you need to define the problems encountered during the log file saving and running process. If a timer is used, it is also a system timer.

The following example shows how to create a windows service program. VS2013 is used as an example:

1. Create a windows Service

Let's see what VS generates.

 

2. Rename Service1, which is the name of the windows service that will run later. Change it to SQLServerSyncService. cs.

Open SQLServerSyncService. cs and add background code

Namespace SQLServerSyncService {public partial class SQLServerSyncService: ServiceBase {private System. timers. 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 database synchronization 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 * 60 + (time2-time1 ). seconds;} No ++; timer1.Start ();} protected override void OnStart (string [] args) {timer1 = new System. timers. timer (); timer1.Elapsed + = new ElapsedEventHandler (timerrenttick); string timestr = XMLConfig. getValue ("appSettings", "add", "SyncInterval", "value"); // synchronization interval timer1.Interval = (int) (float. parse (timestr) * 60*1000); timer1.AutoReset = true; // set whether to execute once (false) or always execute (true) timer1.Enabled = true; // whether to execute System. timers. timer. elapsed event} protected override void OnContinue () {// timer1.Enabled = true when the service is restored;} protected override void OnStop () {timer1.Enabled = false ;}}}

 

3. Add the installer to set the windows service program. You can also write your own code to implement it. But why.

Right-click

Two components are displayed.

Right-click serviceInsraller1, select properties, and change the value of ServiceName to SQLServerSyncService. (This name is the name of a later service)

Right-click serviceProcessInsraller1, select properties, and change the Account value to LocalSystem.

Generate a solution, and finally copy the generated exe program to the easy-to-remember directory. The directory is the path of the Service generated after installation.

 

4. Install the service program. Open the command prompt tool for VS developers. Be sure to run the tool as an administrator. Otherwise, an error occurs: Windows Service Installation exception: System. security. securityException: the source is not found, but some or all Event Logs cannot be searched. Inaccessible logs: Security

Input the command installutil.exe C: \ Server \ SQLServerSyncService.exe, and press ENTER

5. After the service program is installed, start it.

Enter services. msc in the debugging tool or DOC of VS developers above, and press enter to bring up the service dialog box. Find our service program and start it.

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.