Add services for periodic execution of a program or application (Windows service)

Source: Internet
Author: User

Need to be written together with window's setup program






Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.diagnostics;using system.linq;using system.serviceprocess;using system.text;using Inspur.dataexchange.logging;using system.configuration;using Inspur.finix.exceptionmanagement;namespace wenzhouzj.exchange.fromnw{public partial class Service1:servicebase {private System.Timers.Timer timer2; Public Service1 () {this.timer2 = new System.Timers.Timer (); ((System.ComponentModel.ISupportInitialize) (This.timer2)). BeginInit (); This.timer2.Enabled = true; This.timer2.Interval = 1 * 1000; this.timer2.Elapsed + = new System.Timers.ElapsedEventHandler (this.timer2_elapsed); ((System.ComponentModel.ISupportInitialize) (This.timer2)). EndInit (); This. ServiceName = "Wenzhou Quality supervision and data interchange with South Wei, receiving Information Acquisition service"; } protected override void OnStart (string[] args) {//this.timer1.enabled = fAlse; } protected override void OnStop () {this.timer2.Enabled = false; } private void Timer2_elapsed (object sender, System.Timers.ElapsedEventArgs e) {//If the set run time has elapsed String runtime = configurationsettings.appsettings["Runtime"]; if (string. IsNullOrEmpty (runtime) | | Runtime. Equals (DateTime.Now.ToString ("hh:mm"))) {this.timer2.Enabled = false; try {//dataexchange.doexchange (); Periodically executed event} catch (Exception ex) {Exceptionmanager.handle (E x); } This.timer2.Interval = Convert.todouble (system.configuration.configurationsettings.appsettings["Interval" ]) * 60 * 1000; This.timer2.Enabled = true; } } }}


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.